From dcff40c4f3630dfdcefd711b00c2673f3fe2984c Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Fri, 28 Feb 2025 18:45:31 +0000 Subject: [PATCH] refactor(ast_tools): generate layout assertions in multiple crates (#9448) Generate layout assertions in each of the crates containing types tagged `#[ast]`, instead of all the assertions being generated in `oxc_ast` crate. --- .github/.generated_ast_watch_list.yml | 3 + Cargo.lock | 1 - crates/oxc_ast/Cargo.toml | 1 - .../oxc_ast/src/generated/assert_layouts.rs | 383 +----------------- .../src/generated/assert_layouts.rs | 293 ++++++++++++++ crates/oxc_regular_expression/src/lib.rs | 2 + .../oxc_span/src/generated/assert_layouts.rs | 51 +++ crates/oxc_span/src/lib.rs | 2 + .../src/generated/assert_layouts.rs | 85 ++++ crates/oxc_syntax/src/lib.rs | 3 + .../src/generators/assert_layouts.rs | 152 +++++-- 11 files changed, 548 insertions(+), 428 deletions(-) create mode 100644 crates/oxc_regular_expression/src/generated/assert_layouts.rs create mode 100644 crates/oxc_span/src/generated/assert_layouts.rs create mode 100644 crates/oxc_syntax/src/generated/assert_layouts.rs diff --git a/.github/.generated_ast_watch_list.yml b/.github/.generated_ast_watch_list.yml index 18f8c850b8baf..3e455247fdf84 100644 --- a/.github/.generated_ast_watch_list.yml +++ b/.github/.generated_ast_watch_list.yml @@ -25,13 +25,16 @@ src: - 'crates/oxc_ast_visit/src/generated/visit.rs' - 'crates/oxc_ast_visit/src/generated/visit_mut.rs' - 'crates/oxc_regular_expression/src/ast.rs' + - 'crates/oxc_regular_expression/src/generated/assert_layouts.rs' - 'crates/oxc_regular_expression/src/generated/derive_clone_in.rs' - 'crates/oxc_regular_expression/src/generated/derive_content_eq.rs' - 'crates/oxc_regular_expression/src/generated/derive_estree.rs' - 'crates/oxc_regular_expression/src/generated/derive_get_address.rs' + - 'crates/oxc_span/src/generated/assert_layouts.rs' - 'crates/oxc_span/src/generated/derive_estree.rs' - 'crates/oxc_span/src/source_type/mod.rs' - 'crates/oxc_span/src/span.rs' + - 'crates/oxc_syntax/src/generated/assert_layouts.rs' - 'crates/oxc_syntax/src/generated/derive_clone_in.rs' - 'crates/oxc_syntax/src/generated/derive_content_eq.rs' - 'crates/oxc_syntax/src/generated/derive_estree.rs' diff --git a/Cargo.lock b/Cargo.lock index 77338ef03fbe3..01d7181af21fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1586,7 +1586,6 @@ version = "0.53.0" dependencies = [ "bitflags 2.8.0", "cow-utils", - "nonmax", "oxc_allocator", "oxc_ast_macros", "oxc_estree", diff --git a/crates/oxc_ast/Cargo.toml b/crates/oxc_ast/Cargo.toml index 00349c583276b..62a1a9eb338c7 100644 --- a/crates/oxc_ast/Cargo.toml +++ b/crates/oxc_ast/Cargo.toml @@ -28,7 +28,6 @@ oxc_syntax = { workspace = true } bitflags = { workspace = true } cow-utils = { workspace = true } -nonmax = { workspace = true } [features] default = [] diff --git a/crates/oxc_ast/src/generated/assert_layouts.rs b/crates/oxc_ast/src/generated/assert_layouts.rs index c04320eecfc88..c8fcd9221ee59 100644 --- a/crates/oxc_ast/src/generated/assert_layouts.rs +++ b/crates/oxc_ast/src/generated/assert_layouts.rs @@ -1,12 +1,9 @@ // Auto-generated code, DO NOT EDIT DIRECTLY! // To edit this generated file you have to edit `tasks/ast_tools/src/generators/assert_layouts.rs` -use std::mem::{align_of, offset_of, size_of}; - -use nonmax::NonMaxU32; +#![allow(unused_imports)] -use oxc_regular_expression::ast::*; -use oxc_syntax::{reference::ReferenceId, scope::ScopeId, symbol::SymbolId}; +use std::mem::{align_of, offset_of, size_of}; use crate::ast::*; @@ -1400,194 +1397,6 @@ const _: () = { assert!(offset_of!(Comment, position) == 13); assert!(offset_of!(Comment, preceded_by_newline) == 14); assert!(offset_of!(Comment, followed_by_newline) == 15); - - assert!(size_of::() == 4); - assert!(align_of::() == 4); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 4); - assert!(align_of::() == 4); - - assert!(size_of::() == 4); - assert!(align_of::() == 4); - - assert!(size_of::() == 4); - assert!(align_of::() == 4); - - assert!(size_of::() == 8); - assert!(align_of::() == 8); - assert!(offset_of!(Span, start) == 0); - assert!(offset_of!(Span, end) == 4); - - assert!(size_of::() == 3); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 48); - assert!(align_of::() == 8); - assert!(offset_of!(Pattern, span) == 0); - assert!(offset_of!(Pattern, body) == 8); - - assert!(size_of::() == 40); - assert!(align_of::() == 8); - assert!(offset_of!(Disjunction, span) == 0); - assert!(offset_of!(Disjunction, body) == 8); - - assert!(size_of::() == 40); - assert!(align_of::() == 8); - assert!(offset_of!(Alternative, span) == 0); - assert!(offset_of!(Alternative, body) == 8); - - assert!(size_of::() == 16); - assert!(align_of::() == 8); - - assert!(size_of::() == 16); - assert!(align_of::() == 8); - assert!(offset_of!(BoundaryAssertion, span) == 0); - assert!(offset_of!(BoundaryAssertion, kind) == 8); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 56); - assert!(align_of::() == 8); - assert!(offset_of!(LookAroundAssertion, span) == 0); - assert!(offset_of!(LookAroundAssertion, kind) == 8); - assert!(offset_of!(LookAroundAssertion, body) == 16); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 56); - assert!(align_of::() == 8); - assert!(offset_of!(Quantifier, span) == 0); - assert!(offset_of!(Quantifier, min) == 8); - assert!(offset_of!(Quantifier, max) == 16); - assert!(offset_of!(Quantifier, greedy) == 32); - assert!(offset_of!(Quantifier, body) == 40); - - assert!(size_of::() == 16); - assert!(align_of::() == 8); - assert!(offset_of!(Character, span) == 0); - assert!(offset_of!(Character, kind) == 8); - assert!(offset_of!(Character, value) == 12); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 16); - assert!(align_of::() == 8); - assert!(offset_of!(CharacterClassEscape, span) == 0); - assert!(offset_of!(CharacterClassEscape, kind) == 8); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 48); - assert!(align_of::() == 8); - assert!(offset_of!(UnicodePropertyEscape, span) == 0); - assert!(offset_of!(UnicodePropertyEscape, negative) == 8); - assert!(offset_of!(UnicodePropertyEscape, strings) == 9); - assert!(offset_of!(UnicodePropertyEscape, name) == 16); - assert!(offset_of!(UnicodePropertyEscape, value) == 32); - - assert!(size_of::() == 8); - assert!(align_of::() == 8); - assert!(offset_of!(Dot, span) == 0); - - assert!(size_of::() == 48); - assert!(align_of::() == 8); - assert!(offset_of!(CharacterClass, span) == 0); - assert!(offset_of!(CharacterClass, negative) == 8); - assert!(offset_of!(CharacterClass, strings) == 9); - assert!(offset_of!(CharacterClass, kind) == 10); - assert!(offset_of!(CharacterClass, body) == 16); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 16); - assert!(align_of::() == 8); - - assert!(size_of::() == 40); - assert!(align_of::() == 8); - assert!(offset_of!(CharacterClassRange, span) == 0); - assert!(offset_of!(CharacterClassRange, min) == 8); - assert!(offset_of!(CharacterClassRange, max) == 24); - - assert!(size_of::() == 48); - assert!(align_of::() == 8); - assert!(offset_of!(ClassStringDisjunction, span) == 0); - assert!(offset_of!(ClassStringDisjunction, strings) == 8); - assert!(offset_of!(ClassStringDisjunction, body) == 16); - - assert!(size_of::() == 48); - assert!(align_of::() == 8); - assert!(offset_of!(ClassString, span) == 0); - assert!(offset_of!(ClassString, strings) == 8); - assert!(offset_of!(ClassString, body) == 16); - - assert!(size_of::() == 64); - assert!(align_of::() == 8); - assert!(offset_of!(CapturingGroup, span) == 0); - assert!(offset_of!(CapturingGroup, name) == 8); - assert!(offset_of!(CapturingGroup, body) == 24); - - assert!(size_of::() == 64); - assert!(align_of::() == 8); - assert!(offset_of!(IgnoreGroup, span) == 0); - assert!(offset_of!(IgnoreGroup, modifiers) == 8); - assert!(offset_of!(IgnoreGroup, body) == 24); - - assert!(size_of::() == 16); - assert!(align_of::() == 8); - assert!(offset_of!(Modifiers, span) == 0); - assert!(offset_of!(Modifiers, enabling) == 8); - assert!(offset_of!(Modifiers, disabling) == 11); - - assert!(size_of::() == 3); - assert!(align_of::() == 1); - assert!(offset_of!(Modifier, ignore_case) == 0); - assert!(offset_of!(Modifier, multiline) == 1); - assert!(offset_of!(Modifier, sticky) == 2); - - assert!(size_of::() == 16); - assert!(align_of::() == 8); - assert!(offset_of!(IndexedReference, span) == 0); - assert!(offset_of!(IndexedReference, index) == 8); - - assert!(size_of::() == 24); - assert!(align_of::() == 8); - assert!(offset_of!(NamedReference, span) == 0); - assert!(offset_of!(NamedReference, name) == 8); }; #[cfg(target_pointer_width = "32")] @@ -2980,194 +2789,6 @@ const _: () = { assert!(offset_of!(Comment, position) == 13); assert!(offset_of!(Comment, preceded_by_newline) == 14); assert!(offset_of!(Comment, followed_by_newline) == 15); - - assert!(size_of::() == 4); - assert!(align_of::() == 4); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 4); - assert!(align_of::() == 4); - - assert!(size_of::() == 4); - assert!(align_of::() == 4); - - assert!(size_of::() == 4); - assert!(align_of::() == 4); - - assert!(size_of::() == 8); - assert!(align_of::() == 4); - assert!(offset_of!(Span, start) == 0); - assert!(offset_of!(Span, end) == 4); - - assert!(size_of::() == 3); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 32); - assert!(align_of::() == 4); - assert!(offset_of!(Pattern, span) == 0); - assert!(offset_of!(Pattern, body) == 8); - - assert!(size_of::() == 24); - assert!(align_of::() == 4); - assert!(offset_of!(Disjunction, span) == 0); - assert!(offset_of!(Disjunction, body) == 8); - - assert!(size_of::() == 24); - assert!(align_of::() == 4); - assert!(offset_of!(Alternative, span) == 0); - assert!(offset_of!(Alternative, body) == 8); - - assert!(size_of::() == 12); - assert!(align_of::() == 4); - - assert!(size_of::() == 12); - assert!(align_of::() == 4); - assert!(offset_of!(BoundaryAssertion, span) == 0); - assert!(offset_of!(BoundaryAssertion, kind) == 8); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 36); - assert!(align_of::() == 4); - assert!(offset_of!(LookAroundAssertion, span) == 0); - assert!(offset_of!(LookAroundAssertion, kind) == 8); - assert!(offset_of!(LookAroundAssertion, body) == 12); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 48); - assert!(align_of::() == 8); - assert!(offset_of!(Quantifier, span) == 0); - assert!(offset_of!(Quantifier, min) == 8); - assert!(offset_of!(Quantifier, max) == 16); - assert!(offset_of!(Quantifier, greedy) == 32); - assert!(offset_of!(Quantifier, body) == 36); - - assert!(size_of::() == 16); - assert!(align_of::() == 4); - assert!(offset_of!(Character, span) == 0); - assert!(offset_of!(Character, kind) == 8); - assert!(offset_of!(Character, value) == 12); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 12); - assert!(align_of::() == 4); - assert!(offset_of!(CharacterClassEscape, span) == 0); - assert!(offset_of!(CharacterClassEscape, kind) == 8); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 28); - assert!(align_of::() == 4); - assert!(offset_of!(UnicodePropertyEscape, span) == 0); - assert!(offset_of!(UnicodePropertyEscape, negative) == 8); - assert!(offset_of!(UnicodePropertyEscape, strings) == 9); - assert!(offset_of!(UnicodePropertyEscape, name) == 12); - assert!(offset_of!(UnicodePropertyEscape, value) == 20); - - assert!(size_of::() == 8); - assert!(align_of::() == 4); - assert!(offset_of!(Dot, span) == 0); - - assert!(size_of::() == 28); - assert!(align_of::() == 4); - assert!(offset_of!(CharacterClass, span) == 0); - assert!(offset_of!(CharacterClass, negative) == 8); - assert!(offset_of!(CharacterClass, strings) == 9); - assert!(offset_of!(CharacterClass, kind) == 10); - assert!(offset_of!(CharacterClass, body) == 12); - - assert!(size_of::() == 1); - assert!(align_of::() == 1); - - assert!(size_of::() == 8); - assert!(align_of::() == 4); - - assert!(size_of::() == 40); - assert!(align_of::() == 4); - assert!(offset_of!(CharacterClassRange, span) == 0); - assert!(offset_of!(CharacterClassRange, min) == 8); - assert!(offset_of!(CharacterClassRange, max) == 24); - - assert!(size_of::() == 28); - assert!(align_of::() == 4); - assert!(offset_of!(ClassStringDisjunction, span) == 0); - assert!(offset_of!(ClassStringDisjunction, strings) == 8); - assert!(offset_of!(ClassStringDisjunction, body) == 12); - - assert!(size_of::() == 28); - assert!(align_of::() == 4); - assert!(offset_of!(ClassString, span) == 0); - assert!(offset_of!(ClassString, strings) == 8); - assert!(offset_of!(ClassString, body) == 12); - - assert!(size_of::() == 40); - assert!(align_of::() == 4); - assert!(offset_of!(CapturingGroup, span) == 0); - assert!(offset_of!(CapturingGroup, name) == 8); - assert!(offset_of!(CapturingGroup, body) == 16); - - assert!(size_of::() == 48); - assert!(align_of::() == 4); - assert!(offset_of!(IgnoreGroup, span) == 0); - assert!(offset_of!(IgnoreGroup, modifiers) == 8); - assert!(offset_of!(IgnoreGroup, body) == 24); - - assert!(size_of::() == 16); - assert!(align_of::() == 4); - assert!(offset_of!(Modifiers, span) == 0); - assert!(offset_of!(Modifiers, enabling) == 8); - assert!(offset_of!(Modifiers, disabling) == 11); - - assert!(size_of::() == 3); - assert!(align_of::() == 1); - assert!(offset_of!(Modifier, ignore_case) == 0); - assert!(offset_of!(Modifier, multiline) == 1); - assert!(offset_of!(Modifier, sticky) == 2); - - assert!(size_of::() == 12); - assert!(align_of::() == 4); - assert!(offset_of!(IndexedReference, span) == 0); - assert!(offset_of!(IndexedReference, index) == 8); - - assert!(size_of::() == 16); - assert!(align_of::() == 4); - assert!(offset_of!(NamedReference, span) == 0); - assert!(offset_of!(NamedReference, name) == 8); }; #[cfg(not(any(target_pointer_width = "64", target_pointer_width = "32")))] diff --git a/crates/oxc_regular_expression/src/generated/assert_layouts.rs b/crates/oxc_regular_expression/src/generated/assert_layouts.rs new file mode 100644 index 0000000000000..98222e9cda5d6 --- /dev/null +++ b/crates/oxc_regular_expression/src/generated/assert_layouts.rs @@ -0,0 +1,293 @@ +// Auto-generated code, DO NOT EDIT DIRECTLY! +// To edit this generated file you have to edit `tasks/ast_tools/src/generators/assert_layouts.rs` + +#![allow(unused_imports)] + +use std::mem::{align_of, offset_of, size_of}; + +use crate::ast::*; + +#[cfg(target_pointer_width = "64")] +const _: () = { + assert!(size_of::() == 48); + assert!(align_of::() == 8); + assert!(offset_of!(Pattern, span) == 0); + assert!(offset_of!(Pattern, body) == 8); + + assert!(size_of::() == 40); + assert!(align_of::() == 8); + assert!(offset_of!(Disjunction, span) == 0); + assert!(offset_of!(Disjunction, body) == 8); + + assert!(size_of::() == 40); + assert!(align_of::() == 8); + assert!(offset_of!(Alternative, span) == 0); + assert!(offset_of!(Alternative, body) == 8); + + assert!(size_of::() == 16); + assert!(align_of::() == 8); + + assert!(size_of::() == 16); + assert!(align_of::() == 8); + assert!(offset_of!(BoundaryAssertion, span) == 0); + assert!(offset_of!(BoundaryAssertion, kind) == 8); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 56); + assert!(align_of::() == 8); + assert!(offset_of!(LookAroundAssertion, span) == 0); + assert!(offset_of!(LookAroundAssertion, kind) == 8); + assert!(offset_of!(LookAroundAssertion, body) == 16); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 56); + assert!(align_of::() == 8); + assert!(offset_of!(Quantifier, span) == 0); + assert!(offset_of!(Quantifier, min) == 8); + assert!(offset_of!(Quantifier, max) == 16); + assert!(offset_of!(Quantifier, greedy) == 32); + assert!(offset_of!(Quantifier, body) == 40); + + assert!(size_of::() == 16); + assert!(align_of::() == 8); + assert!(offset_of!(Character, span) == 0); + assert!(offset_of!(Character, kind) == 8); + assert!(offset_of!(Character, value) == 12); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 16); + assert!(align_of::() == 8); + assert!(offset_of!(CharacterClassEscape, span) == 0); + assert!(offset_of!(CharacterClassEscape, kind) == 8); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 48); + assert!(align_of::() == 8); + assert!(offset_of!(UnicodePropertyEscape, span) == 0); + assert!(offset_of!(UnicodePropertyEscape, negative) == 8); + assert!(offset_of!(UnicodePropertyEscape, strings) == 9); + assert!(offset_of!(UnicodePropertyEscape, name) == 16); + assert!(offset_of!(UnicodePropertyEscape, value) == 32); + + assert!(size_of::() == 8); + assert!(align_of::() == 8); + assert!(offset_of!(Dot, span) == 0); + + assert!(size_of::() == 48); + assert!(align_of::() == 8); + assert!(offset_of!(CharacterClass, span) == 0); + assert!(offset_of!(CharacterClass, negative) == 8); + assert!(offset_of!(CharacterClass, strings) == 9); + assert!(offset_of!(CharacterClass, kind) == 10); + assert!(offset_of!(CharacterClass, body) == 16); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 16); + assert!(align_of::() == 8); + + assert!(size_of::() == 40); + assert!(align_of::() == 8); + assert!(offset_of!(CharacterClassRange, span) == 0); + assert!(offset_of!(CharacterClassRange, min) == 8); + assert!(offset_of!(CharacterClassRange, max) == 24); + + assert!(size_of::() == 48); + assert!(align_of::() == 8); + assert!(offset_of!(ClassStringDisjunction, span) == 0); + assert!(offset_of!(ClassStringDisjunction, strings) == 8); + assert!(offset_of!(ClassStringDisjunction, body) == 16); + + assert!(size_of::() == 48); + assert!(align_of::() == 8); + assert!(offset_of!(ClassString, span) == 0); + assert!(offset_of!(ClassString, strings) == 8); + assert!(offset_of!(ClassString, body) == 16); + + assert!(size_of::() == 64); + assert!(align_of::() == 8); + assert!(offset_of!(CapturingGroup, span) == 0); + assert!(offset_of!(CapturingGroup, name) == 8); + assert!(offset_of!(CapturingGroup, body) == 24); + + assert!(size_of::() == 64); + assert!(align_of::() == 8); + assert!(offset_of!(IgnoreGroup, span) == 0); + assert!(offset_of!(IgnoreGroup, modifiers) == 8); + assert!(offset_of!(IgnoreGroup, body) == 24); + + assert!(size_of::() == 16); + assert!(align_of::() == 8); + assert!(offset_of!(Modifiers, span) == 0); + assert!(offset_of!(Modifiers, enabling) == 8); + assert!(offset_of!(Modifiers, disabling) == 11); + + assert!(size_of::() == 3); + assert!(align_of::() == 1); + assert!(offset_of!(Modifier, ignore_case) == 0); + assert!(offset_of!(Modifier, multiline) == 1); + assert!(offset_of!(Modifier, sticky) == 2); + + assert!(size_of::() == 16); + assert!(align_of::() == 8); + assert!(offset_of!(IndexedReference, span) == 0); + assert!(offset_of!(IndexedReference, index) == 8); + + assert!(size_of::() == 24); + assert!(align_of::() == 8); + assert!(offset_of!(NamedReference, span) == 0); + assert!(offset_of!(NamedReference, name) == 8); +}; + +#[cfg(target_pointer_width = "32")] +const _: () = { + assert!(size_of::() == 32); + assert!(align_of::() == 4); + assert!(offset_of!(Pattern, span) == 0); + assert!(offset_of!(Pattern, body) == 8); + + assert!(size_of::() == 24); + assert!(align_of::() == 4); + assert!(offset_of!(Disjunction, span) == 0); + assert!(offset_of!(Disjunction, body) == 8); + + assert!(size_of::() == 24); + assert!(align_of::() == 4); + assert!(offset_of!(Alternative, span) == 0); + assert!(offset_of!(Alternative, body) == 8); + + assert!(size_of::() == 12); + assert!(align_of::() == 4); + + assert!(size_of::() == 12); + assert!(align_of::() == 4); + assert!(offset_of!(BoundaryAssertion, span) == 0); + assert!(offset_of!(BoundaryAssertion, kind) == 8); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 36); + assert!(align_of::() == 4); + assert!(offset_of!(LookAroundAssertion, span) == 0); + assert!(offset_of!(LookAroundAssertion, kind) == 8); + assert!(offset_of!(LookAroundAssertion, body) == 12); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 48); + assert!(align_of::() == 8); + assert!(offset_of!(Quantifier, span) == 0); + assert!(offset_of!(Quantifier, min) == 8); + assert!(offset_of!(Quantifier, max) == 16); + assert!(offset_of!(Quantifier, greedy) == 32); + assert!(offset_of!(Quantifier, body) == 36); + + assert!(size_of::() == 16); + assert!(align_of::() == 4); + assert!(offset_of!(Character, span) == 0); + assert!(offset_of!(Character, kind) == 8); + assert!(offset_of!(Character, value) == 12); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 12); + assert!(align_of::() == 4); + assert!(offset_of!(CharacterClassEscape, span) == 0); + assert!(offset_of!(CharacterClassEscape, kind) == 8); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 28); + assert!(align_of::() == 4); + assert!(offset_of!(UnicodePropertyEscape, span) == 0); + assert!(offset_of!(UnicodePropertyEscape, negative) == 8); + assert!(offset_of!(UnicodePropertyEscape, strings) == 9); + assert!(offset_of!(UnicodePropertyEscape, name) == 12); + assert!(offset_of!(UnicodePropertyEscape, value) == 20); + + assert!(size_of::() == 8); + assert!(align_of::() == 4); + assert!(offset_of!(Dot, span) == 0); + + assert!(size_of::() == 28); + assert!(align_of::() == 4); + assert!(offset_of!(CharacterClass, span) == 0); + assert!(offset_of!(CharacterClass, negative) == 8); + assert!(offset_of!(CharacterClass, strings) == 9); + assert!(offset_of!(CharacterClass, kind) == 10); + assert!(offset_of!(CharacterClass, body) == 12); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 8); + assert!(align_of::() == 4); + + assert!(size_of::() == 40); + assert!(align_of::() == 4); + assert!(offset_of!(CharacterClassRange, span) == 0); + assert!(offset_of!(CharacterClassRange, min) == 8); + assert!(offset_of!(CharacterClassRange, max) == 24); + + assert!(size_of::() == 28); + assert!(align_of::() == 4); + assert!(offset_of!(ClassStringDisjunction, span) == 0); + assert!(offset_of!(ClassStringDisjunction, strings) == 8); + assert!(offset_of!(ClassStringDisjunction, body) == 12); + + assert!(size_of::() == 28); + assert!(align_of::() == 4); + assert!(offset_of!(ClassString, span) == 0); + assert!(offset_of!(ClassString, strings) == 8); + assert!(offset_of!(ClassString, body) == 12); + + assert!(size_of::() == 40); + assert!(align_of::() == 4); + assert!(offset_of!(CapturingGroup, span) == 0); + assert!(offset_of!(CapturingGroup, name) == 8); + assert!(offset_of!(CapturingGroup, body) == 16); + + assert!(size_of::() == 48); + assert!(align_of::() == 4); + assert!(offset_of!(IgnoreGroup, span) == 0); + assert!(offset_of!(IgnoreGroup, modifiers) == 8); + assert!(offset_of!(IgnoreGroup, body) == 24); + + assert!(size_of::() == 16); + assert!(align_of::() == 4); + assert!(offset_of!(Modifiers, span) == 0); + assert!(offset_of!(Modifiers, enabling) == 8); + assert!(offset_of!(Modifiers, disabling) == 11); + + assert!(size_of::() == 3); + assert!(align_of::() == 1); + assert!(offset_of!(Modifier, ignore_case) == 0); + assert!(offset_of!(Modifier, multiline) == 1); + assert!(offset_of!(Modifier, sticky) == 2); + + assert!(size_of::() == 12); + assert!(align_of::() == 4); + assert!(offset_of!(IndexedReference, span) == 0); + assert!(offset_of!(IndexedReference, index) == 8); + + assert!(size_of::() == 16); + assert!(align_of::() == 4); + assert!(offset_of!(NamedReference, span) == 0); + assert!(offset_of!(NamedReference, name) == 8); +}; + +#[cfg(not(any(target_pointer_width = "64", target_pointer_width = "32")))] +const _: () = panic!("Platforms with pointer width other than 64 or 32 bit are not supported"); diff --git a/crates/oxc_regular_expression/src/lib.rs b/crates/oxc_regular_expression/src/lib.rs index 55c8a753700c7..61c1234b83d75 100644 --- a/crates/oxc_regular_expression/src/lib.rs +++ b/crates/oxc_regular_expression/src/lib.rs @@ -7,6 +7,8 @@ mod parser; mod surrogate_pair; mod generated { + #[cfg(debug_assertions)] + pub mod assert_layouts; mod derive_clone_in; mod derive_content_eq; #[cfg(feature = "serialize")] diff --git a/crates/oxc_span/src/generated/assert_layouts.rs b/crates/oxc_span/src/generated/assert_layouts.rs new file mode 100644 index 0000000000000..0b7e12f99e74d --- /dev/null +++ b/crates/oxc_span/src/generated/assert_layouts.rs @@ -0,0 +1,51 @@ +// Auto-generated code, DO NOT EDIT DIRECTLY! +// To edit this generated file you have to edit `tasks/ast_tools/src/generators/assert_layouts.rs` + +#![allow(unused_imports)] + +use std::mem::{align_of, offset_of, size_of}; + +use crate::*; + +#[cfg(target_pointer_width = "64")] +const _: () = { + assert!(size_of::() == 8); + assert!(align_of::() == 8); + assert!(offset_of!(Span, start) == 0); + assert!(offset_of!(Span, end) == 4); + + assert!(size_of::() == 3); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); +}; + +#[cfg(target_pointer_width = "32")] +const _: () = { + assert!(size_of::() == 8); + assert!(align_of::() == 4); + assert!(offset_of!(Span, start) == 0); + assert!(offset_of!(Span, end) == 4); + + assert!(size_of::() == 3); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); +}; + +#[cfg(not(any(target_pointer_width = "64", target_pointer_width = "32")))] +const _: () = panic!("Platforms with pointer width other than 64 or 32 bit are not supported"); diff --git a/crates/oxc_span/src/lib.rs b/crates/oxc_span/src/lib.rs index fbf626bc08cce..813497418794b 100644 --- a/crates/oxc_span/src/lib.rs +++ b/crates/oxc_span/src/lib.rs @@ -21,6 +21,8 @@ pub use crate::{ }; mod generated { + #[cfg(debug_assertions)] + pub mod assert_layouts; #[cfg(feature = "serialize")] pub mod derive_estree; } diff --git a/crates/oxc_syntax/src/generated/assert_layouts.rs b/crates/oxc_syntax/src/generated/assert_layouts.rs new file mode 100644 index 0000000000000..f4b27d2f92392 --- /dev/null +++ b/crates/oxc_syntax/src/generated/assert_layouts.rs @@ -0,0 +1,85 @@ +// Auto-generated code, DO NOT EDIT DIRECTLY! +// To edit this generated file you have to edit `tasks/ast_tools/src/generators/assert_layouts.rs` + +#![allow(unused_imports)] + +use std::mem::{align_of, offset_of, size_of}; + +use nonmax::NonMaxU32; + +use crate::{number::*, operator::*, reference::*, scope::*, symbol::*}; + +#[cfg(target_pointer_width = "64")] +const _: () = { + assert!(size_of::() == 4); + assert!(align_of::() == 4); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 4); + assert!(align_of::() == 4); + + assert!(size_of::() == 4); + assert!(align_of::() == 4); + + assert!(size_of::() == 4); + assert!(align_of::() == 4); +}; + +#[cfg(target_pointer_width = "32")] +const _: () = { + assert!(size_of::() == 4); + assert!(align_of::() == 4); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 1); + assert!(align_of::() == 1); + + assert!(size_of::() == 4); + assert!(align_of::() == 4); + + assert!(size_of::() == 4); + assert!(align_of::() == 4); + + assert!(size_of::() == 4); + assert!(align_of::() == 4); +}; + +#[cfg(not(any(target_pointer_width = "64", target_pointer_width = "32")))] +const _: () = panic!("Platforms with pointer width other than 64 or 32 bit are not supported"); diff --git a/crates/oxc_syntax/src/lib.rs b/crates/oxc_syntax/src/lib.rs index c4ae9230e6041..b53c48ea12284 100644 --- a/crates/oxc_syntax/src/lib.rs +++ b/crates/oxc_syntax/src/lib.rs @@ -18,7 +18,10 @@ pub mod reference; pub mod scope; pub mod symbol; pub mod xml_entities; + mod generated { + #[cfg(debug_assertions)] + pub mod assert_layouts; mod derive_clone_in; mod derive_content_eq; #[cfg(feature = "serialize")] diff --git a/tasks/ast_tools/src/generators/assert_layouts.rs b/tasks/ast_tools/src/generators/assert_layouts.rs index 8d8b77f5f4ba2..321ea69a05605 100644 --- a/tasks/ast_tools/src/generators/assert_layouts.rs +++ b/tasks/ast_tools/src/generators/assert_layouts.rs @@ -5,16 +5,18 @@ //! Calculate each separately, and generate assertions for each. use std::{ + borrow::Cow, cmp::{max, min}, num, }; use proc_macro2::TokenStream; use quote::quote; +use rustc_hash::FxHashMap; use syn::Ident; use crate::{ - AST_CRATE_PATH, Codegen, Generator, + Codegen, Generator, output::{Output, output_path}, schema::{ Def, Discriminant, EnumDef, PrimitiveDef, Schema, StructDef, TypeDef, TypeId, Visibility, @@ -39,37 +41,8 @@ impl Generator for AssertLayouts { } /// Generate assertions that calculated layouts are correct. - fn generate(&self, schema: &Schema, _codegen: &Codegen) -> Output { - let (assertions_64, assertions_32): (TokenStream, TokenStream) = - schema.types.iter().map(generate_layout_assertions).unzip(); - - let output = quote! { - use std::mem::{align_of, offset_of, size_of}; - - ///@@line_break - use nonmax::NonMaxU32; - - ///@@line_break - use oxc_regular_expression::ast::*; - use oxc_syntax::{reference::ReferenceId, scope::ScopeId, symbol::SymbolId}; - - ///@@line_break - use crate::ast::*; - - ///@@line_break - #[cfg(target_pointer_width = "64")] - const _: () = { #assertions_64 }; - - ///@@line_break - #[cfg(target_pointer_width = "32")] - const _: () = { #assertions_32 }; - - ///@@line_break - #[cfg(not(any(target_pointer_width = "64", target_pointer_width = "32")))] - const _: () = panic!("Platforms with pointer width other than 64 or 32 bit are not supported"); - }; - - Output::Rust { path: output_path(AST_CRATE_PATH, "assert_layouts.rs"), tokens: output } + fn generate_many(&self, schema: &Schema, _codegen: &Codegen) -> Vec { + generate_assertions(schema) } } @@ -417,20 +390,53 @@ fn calculate_layout_for_primitive(primitive_def: &PrimitiveDef) -> Layout { } } -/// Generate layout assertions for a type -fn generate_layout_assertions( +/// Generate layout assertions for all types. +fn generate_assertions(schema: &Schema) -> Vec { + let mut assertions = FxHashMap::default(); + + for type_def in &schema.types { + generate_layout_assertions(type_def, &mut assertions, schema); + } + + assertions + .into_iter() + .map(|(krate, (assertions_64, assertions_32))| { + let output = template(krate, &assertions_64, &assertions_32); + + let crate_path = if krate.starts_with("napi/") { + Cow::Borrowed(krate) + } else { + Cow::Owned(format!("crates/{krate}")) + }; + Output::Rust { path: output_path(&crate_path, "assert_layouts.rs"), tokens: output } + }) + .collect() +} + +/// Generate layout assertions for a type. +fn generate_layout_assertions<'s>( type_def: &TypeDef, -) -> (/* 64 bit */ TokenStream, /* 32 bit */ TokenStream) { + assertions: &mut FxHashMap<&'s str, (/* 64 bit */ TokenStream, /* 32 bit */ TokenStream)>, + schema: &'s Schema, +) { match type_def { - TypeDef::Struct(struct_def) => generate_layout_assertions_for_struct(struct_def), - TypeDef::Enum(enum_def) => generate_layout_assertions_for_enum(enum_def), - _ => (quote!(), quote!()), + TypeDef::Struct(struct_def) => { + generate_layout_assertions_for_struct(struct_def, assertions, schema); + } + TypeDef::Enum(enum_def) => { + generate_layout_assertions_for_enum(enum_def, assertions, schema); + } + _ => {} } } /// Generate layout assertions for a struct. /// This includes size and alignment assertions, plus assertions about offset of fields. -fn generate_layout_assertions_for_struct(struct_def: &StructDef) -> (TokenStream, TokenStream) { +fn generate_layout_assertions_for_struct<'s>( + struct_def: &StructDef, + assertions: &mut FxHashMap<&'s str, (/* 64 bit */ TokenStream, /* 32 bit */ TokenStream)>, + schema: &'s Schema, +) { fn r#gen(struct_def: &StructDef, is_64: bool, struct_ident: &Ident) -> TokenStream { let layout = if is_64 { &struct_def.layout.layout_64 } else { &struct_def.layout.layout_32 }; @@ -458,18 +464,27 @@ fn generate_layout_assertions_for_struct(struct_def: &StructDef) -> (TokenStream } } + let (assertions_64, assertions_32) = + assertions.entry(struct_def.file(schema).krate()).or_default(); + let ident = struct_def.ident(); - (r#gen(struct_def, true, &ident), r#gen(struct_def, false, &ident)) + assertions_64.extend(r#gen(struct_def, true, &ident)); + assertions_32.extend(r#gen(struct_def, false, &ident)); } /// Generate layout assertions for an enum. /// This is just size and alignment assertions. -fn generate_layout_assertions_for_enum(enum_def: &EnumDef) -> (TokenStream, TokenStream) { +fn generate_layout_assertions_for_enum<'s>( + enum_def: &EnumDef, + assertions: &mut FxHashMap<&'s str, (/* 64 bit */ TokenStream, /* 32 bit */ TokenStream)>, + schema: &'s Schema, +) { + let (assertions_64, assertions_32) = + assertions.entry(enum_def.file(schema).krate()).or_default(); + let ident = enum_def.ident(); - ( - generate_size_align_assertions(&enum_def.layout.layout_64, &ident), - generate_size_align_assertions(&enum_def.layout.layout_32, &ident), - ) + assertions_64.extend(generate_size_align_assertions(&enum_def.layout.layout_64, &ident)); + assertions_32.extend(generate_size_align_assertions(&enum_def.layout.layout_32, &ident)); } /// Generate size and alignment assertions for a type. @@ -482,3 +497,50 @@ fn generate_size_align_assertions(layout: &PlatformLayout, ident: &Ident) -> Tok assert!(align_of::<#ident>() == #align); } } + +/// Generate output for a crate. +fn template(krate: &str, assertions_64: &TokenStream, assertions_32: &TokenStream) -> TokenStream { + #[expect(clippy::match_same_arms)] + let imports = match krate { + "oxc_ast" => quote! { + use crate::ast::*; + }, + "oxc_regular_expression" => quote! { + use crate::ast::*; + }, + "oxc_span" => quote! { + use crate::*; + }, + "oxc_syntax" => quote! { + use nonmax::NonMaxU32; + + ///@@line_break + use crate::{number::*, operator::*, reference::*, scope::*, symbol::*}; + }, + _ => quote! { + use crate::*; + }, + }; + + quote! { + #![allow(unused_imports)] + + ///@@line_break + use std::mem::{align_of, offset_of, size_of}; + + ///@@line_break + #imports + + ///@@line_break + #[cfg(target_pointer_width = "64")] + const _: () = { #assertions_64 }; + + ///@@line_break + #[cfg(target_pointer_width = "32")] + const _: () = { #assertions_32 }; + + ///@@line_break + #[cfg(not(any(target_pointer_width = "64", target_pointer_width = "32")))] + const _: () = panic!("Platforms with pointer width other than 64 or 32 bit are not supported"); + } +}