Skip to content

Commit

Permalink
refactor(syntax): add #[ast] attr to semantic ID types
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Feb 3, 2025
1 parent bd2f2e1 commit e219e3f
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .github/.generated_ast_watch_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ src:
- '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'
- 'crates/oxc_syntax/src/lib.rs'
- 'crates/oxc_syntax/src/number.rs'
- 'crates/oxc_syntax/src/operator.rs'
- 'crates/oxc_syntax/src/reference.rs'
- 'crates/oxc_syntax/src/scope.rs'
- 'crates/oxc_syntax/src/symbol.rs'
- 'npm/oxc-types/types.d.ts'
- 'tasks/ast_tools/src/**'
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/oxc_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ oxc_syntax = { workspace = true }

bitflags = { workspace = true }
cow-utils = { workspace = true }
nonmax = { workspace = true }
num-bigint = { workspace = true }
num-traits = { workspace = true }

Expand Down
27 changes: 27 additions & 0 deletions crates/oxc_ast/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

use std::mem::{align_of, offset_of, size_of};

use nonmax::NonMaxU32;

use oxc_regular_expression::ast::*;
use oxc_syntax::{reference::ReferenceId, scope::ScopeId, symbol::SymbolId};

use crate::ast::*;

Expand Down Expand Up @@ -1393,6 +1396,9 @@ const _: () = {
assert!(offset_of!(Comment, preceded_by_newline) == 14);
assert!(offset_of!(Comment, followed_by_newline) == 15);

assert!(size_of::<NonMaxU32>() == 4);
assert!(align_of::<NonMaxU32>() == 4);

assert!(size_of::<NumberBase>() == 1);
assert!(align_of::<NumberBase>() == 1);

Expand All @@ -1414,6 +1420,15 @@ const _: () = {
assert!(size_of::<UpdateOperator>() == 1);
assert!(align_of::<UpdateOperator>() == 1);

assert!(size_of::<ScopeId>() == 4);
assert!(align_of::<ScopeId>() == 4);

assert!(size_of::<SymbolId>() == 4);
assert!(align_of::<SymbolId>() == 4);

assert!(size_of::<ReferenceId>() == 4);
assert!(align_of::<ReferenceId>() == 4);

assert!(size_of::<Span>() == 8);
assert!(align_of::<Span>() == 8);
assert!(offset_of!(Span, start) == 0);
Expand Down Expand Up @@ -2959,6 +2974,9 @@ const _: () = {
assert!(offset_of!(Comment, preceded_by_newline) == 14);
assert!(offset_of!(Comment, followed_by_newline) == 15);

assert!(size_of::<NonMaxU32>() == 4);
assert!(align_of::<NonMaxU32>() == 4);

assert!(size_of::<NumberBase>() == 1);
assert!(align_of::<NumberBase>() == 1);

Expand All @@ -2980,6 +2998,15 @@ const _: () = {
assert!(size_of::<UpdateOperator>() == 1);
assert!(align_of::<UpdateOperator>() == 1);

assert!(size_of::<ScopeId>() == 4);
assert!(align_of::<ScopeId>() == 4);

assert!(size_of::<SymbolId>() == 4);
assert!(align_of::<SymbolId>() == 4);

assert!(size_of::<ReferenceId>() == 4);
assert!(align_of::<ReferenceId>() == 4);

assert!(size_of::<Span>() == 8);
assert!(align_of::<Span>() == 4);
assert!(offset_of!(Span, start) == 0);
Expand Down
10 changes: 10 additions & 0 deletions crates/oxc_syntax/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//! Common code for JavaScript Syntax
#![warn(missing_docs)]

use std::num::NonZeroU32;

use oxc_ast_macros::ast;

pub mod class;
pub mod es_target;
pub mod identifier;
Expand All @@ -19,3 +24,8 @@ mod generated {
#[cfg(feature = "serialize")]
mod derive_estree;
}

/// Dummy type to communicate the content of `nonmax::NonMaxU32` to `oxc_ast_tools`.
#[ast(foreign = NonMaxU32)]
#[expect(dead_code)]
struct NonMaxU32Alias(NonZeroU32);
3 changes: 3 additions & 0 deletions crates/oxc_syntax/src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use serde::{Serialize, Serializer};

use crate::{node::NodeId, symbol::SymbolId};

use oxc_ast_macros::ast;

#[ast]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct ReferenceId(NonMaxU32);

Expand Down
3 changes: 3 additions & 0 deletions crates/oxc_syntax/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use oxc_index::Idx;
#[cfg(feature = "serialize")]
use serde::{Serialize, Serializer};

use oxc_ast_macros::ast;

#[ast]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct ScopeId(NonMaxU32);

Expand Down
3 changes: 3 additions & 0 deletions crates/oxc_syntax/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use oxc_index::Idx;
#[cfg(feature = "serialize")]
use serde::{Serialize, Serializer};

use oxc_ast_macros::ast;

#[ast]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct SymbolId(NonMaxU32);

Expand Down
9 changes: 4 additions & 5 deletions tasks/ast_tools/src/generators/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ impl Generator for AssertLayouts {
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::*;
Expand Down Expand Up @@ -337,8 +341,6 @@ fn calculate_layout_for_cell(type_id: TypeId, schema: &mut Schema) -> Layout {
///
/// Primitives have varying layouts. Some have niches, most don't.
fn calculate_layout_for_primitive(primitive_def: &PrimitiveDef) -> Layout {
// `ScopeId`, `SymbolId` and `ReferenceId` are a `NonZeroU32`, with a niche for 0
let semantic_id_layout = Layout::from_size_align_niche(4, 4, Niche::new(0, 4, true, 1));
// `&str` and `Atom` are a `NonNull` pointer + `usize` pair. Niche for 0 on the pointer field
let str_layout = Layout {
layout_64: PlatformLayout::from_size_align_niche(16, 8, Niche::new(0, 8, true, 1)),
Expand Down Expand Up @@ -394,9 +396,6 @@ fn calculate_layout_for_primitive(primitive_def: &PrimitiveDef) -> Layout {
panic!("Cannot calculate alignment for `NonZeroI128`. It differs depending on Rust version.")
}
"NonZeroIsize" => non_zero_usize_layout,
"ScopeId" => semantic_id_layout.clone(),
"SymbolId" => semantic_id_layout.clone(),
"ReferenceId" => semantic_id_layout,
"PointerAlign" => Layout {
layout_64: PlatformLayout::from_size_align(0, 8),
layout_32: PlatformLayout::from_size_align(0, 4),
Expand Down
4 changes: 4 additions & 0 deletions tasks/ast_tools/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,12 @@ static SOURCE_PATHS: &[&str] = &[
"crates/oxc_ast/src/ast/jsx.rs",
"crates/oxc_ast/src/ast/ts.rs",
"crates/oxc_ast/src/ast/comment.rs",
"crates/oxc_syntax/src/lib.rs",
"crates/oxc_syntax/src/number.rs",
"crates/oxc_syntax/src/operator.rs",
"crates/oxc_syntax/src/scope.rs",
"crates/oxc_syntax/src/symbol.rs",
"crates/oxc_syntax/src/reference.rs",
"crates/oxc_span/src/span/types.rs",
"crates/oxc_span/src/source_type/mod.rs",
"crates/oxc_regular_expression/src/ast.rs",
Expand Down
3 changes: 0 additions & 3 deletions tasks/ast_tools/src/parse/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@ impl<'c> Parser<'c> {
"NonZeroIsize" => primitive("NonZeroIsize"),
"&str" => primitive("&str"),
"Atom" => primitive("Atom"),
"ScopeId" => primitive("ScopeId"),
"SymbolId" => primitive("SymbolId"),
"ReferenceId" => primitive("ReferenceId"),
// TODO: Remove the need for this by adding
// `#[cfg_attr(target_pointer_width = "64", repr(align(8)))]` to all AST types
"PointerAlign" => primitive("PointerAlign"),
Expand Down

0 comments on commit e219e3f

Please sign in to comment.