Skip to content

Commit

Permalink
feat(ast_codegen): process AST-related syntax types. (#4694)
Browse files Browse the repository at this point in the history
  • Loading branch information
rzvxa committed Aug 6, 2024
1 parent b04dc3d commit 82e2f6b
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 23 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ jobs:
with:
filters: |
src:
- 'crates/oxc_ast/src/ast/**'
- 'crates/oxc_ast/src/ast/*' # single star is intentional
- 'crates/oxc_ast/src/generated/**' # to potentially cause CI error if generated files are edited manually
- 'crates/oxc_syntax/src/number.rs'
- 'crates/oxc_syntax/src/operator.rs'
- 'tasks/ast_codegen/src/**'
- uses: Boshen/setup-rust@main
Expand Down
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.

29 changes: 29 additions & 0 deletions crates/oxc_ast/src/generated/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use std::mem::{align_of, offset_of, size_of};

use crate::ast::*;
use oxc_syntax::{number::*, operator::*};

#[cfg(target_pointer_width = "64")]
const _: () = {
Expand Down Expand Up @@ -1124,6 +1125,20 @@ const _: () = {
assert!(align_of::<JSXText>() == 8usize);
assert!(offset_of!(JSXText, span) == 0usize);
assert!(offset_of!(JSXText, value) == 8usize);
assert!(size_of::<NumberBase>() == 1usize);
assert!(align_of::<NumberBase>() == 1usize);
assert!(size_of::<BigintBase>() == 1usize);
assert!(align_of::<BigintBase>() == 1usize);
assert!(size_of::<AssignmentOperator>() == 1usize);
assert!(align_of::<AssignmentOperator>() == 1usize);
assert!(size_of::<BinaryOperator>() == 1usize);
assert!(align_of::<BinaryOperator>() == 1usize);
assert!(size_of::<LogicalOperator>() == 1usize);
assert!(align_of::<LogicalOperator>() == 1usize);
assert!(size_of::<UnaryOperator>() == 1usize);
assert!(align_of::<UnaryOperator>() == 1usize);
assert!(size_of::<UpdateOperator>() == 1usize);
assert!(align_of::<UpdateOperator>() == 1usize);
};

#[cfg(target_pointer_width = "32")]
Expand Down Expand Up @@ -2245,6 +2260,20 @@ const _: () = {
assert!(align_of::<JSXText>() == 4usize);
assert!(offset_of!(JSXText, span) == 0usize);
assert!(offset_of!(JSXText, value) == 8usize);
assert!(size_of::<NumberBase>() == 1usize);
assert!(align_of::<NumberBase>() == 1usize);
assert!(size_of::<BigintBase>() == 1usize);
assert!(align_of::<BigintBase>() == 1usize);
assert!(size_of::<AssignmentOperator>() == 1usize);
assert!(align_of::<AssignmentOperator>() == 1usize);
assert!(size_of::<BinaryOperator>() == 1usize);
assert!(align_of::<BinaryOperator>() == 1usize);
assert!(size_of::<LogicalOperator>() == 1usize);
assert!(align_of::<LogicalOperator>() == 1usize);
assert!(size_of::<UnaryOperator>() == 1usize);
assert!(align_of::<UnaryOperator>() == 1usize);
assert!(size_of::<UpdateOperator>() == 1usize);
assert!(align_of::<UpdateOperator>() == 1usize);
};

#[cfg(not(any(target_pointer_width = "64", target_pointer_width = "32")))]
Expand Down
5 changes: 3 additions & 2 deletions crates/oxc_syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ workspace = true
doctest = false

[dependencies]
oxc_index = { workspace = true }
oxc_span = { workspace = true }
oxc_index = { workspace = true }
oxc_span = { workspace = true }
oxc_ast_macros = { workspace = true }

unicode-id-start = { workspace = true }
bitflags = { workspace = true }
Expand Down
6 changes: 4 additions & 2 deletions crates/oxc_syntax/src/number.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#[repr(u8)]
use oxc_ast_macros::ast;

#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NumberBase {
Float = 0,
Expand All @@ -14,7 +16,7 @@ impl NumberBase {
}
}

#[repr(u8)]
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BigintBase {
Decimal = 0,
Expand Down
12 changes: 7 additions & 5 deletions crates/oxc_syntax/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ use serde::Serialize;
#[cfg(feature = "serialize")]
use tsify::Tsify;

use oxc_ast_macros::ast;

use crate::precedence::{GetPrecedence, Precedence};

#[repr(u8)]
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum AssignmentOperator {
Expand Down Expand Up @@ -87,7 +89,7 @@ impl AssignmentOperator {
}
}

#[repr(u8)]
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
pub enum BinaryOperator {
Expand Down Expand Up @@ -275,7 +277,7 @@ impl GetPrecedence for BinaryOperator {
}
}

#[repr(u8)]
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "serialize", derive(Tsify))]
Expand Down Expand Up @@ -316,7 +318,7 @@ impl GetPrecedence for LogicalOperator {
}
}

#[repr(u8)]
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "serialize", derive(Tsify))]
Expand Down Expand Up @@ -370,7 +372,7 @@ impl UnaryOperator {
}
}

#[repr(u8)]
#[ast]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize))]
#[cfg_attr(feature = "serialize", derive(Tsify))]
Expand Down
2 changes: 2 additions & 0 deletions tasks/ast_codegen/src/generators/assert_layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ impl Generator for AssertLayouts {
endl!();

use crate::ast::*;
use oxc_syntax::{number::*, operator::*};


endl!();

Expand Down
15 changes: 10 additions & 5 deletions tasks/ast_codegen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const AST_CRATE: &str = "crates/oxc_ast";
const AST_SYNTAX: &str = "crates/oxc_syntax";
#[allow(dead_code)]
const AST_MACROS_CRATE: &str = "crates/oxc_ast_macros";

Expand Down Expand Up @@ -218,9 +219,8 @@ impl AstCodegen {
.map(rust_ast::Module::from)
.map(rust_ast::Module::load)
.map_ok(rust_ast::Module::expand)
.flatten()
.map_ok(rust_ast::Module::analyze)
.collect::<Result<Result<Vec<_>>>>()??;
.map_ok(|it| it.map(rust_ast::Module::analyze))
.collect::<Result<Result<Result<Vec<_>>>>>()???;

// early passes
let ctx = {
Expand All @@ -244,11 +244,16 @@ impl AstCodegen {
}

fn files() -> impl std::iter::Iterator<Item = String> {
fn path(path: &str) -> String {
fn ast(path: &str) -> String {
format!("{AST_CRATE}/src/ast/{path}.rs")
}

vec![path("literal"), path("js"), path("ts"), path("jsx")].into_iter()
fn syntax(path: &str) -> String {
format!("{AST_SYNTAX}/src/{path}.rs")
}

vec![ast("literal"), ast("js"), ast("ts"), ast("jsx"), syntax("number"), syntax("operator")]
.into_iter()
}

fn write_generated_streams(
Expand Down
13 changes: 5 additions & 8 deletions tasks/ast_codegen/src/passes/calc_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,22 +351,19 @@ lazy_static! {
64 => Layout::wide_ptr_64(),
32 => Layout::wide_ptr_32(),
},
// External Bumpalo type
Vec: {
64 => Layout::known(32, 8, 1),
32 => Layout::known(16, 4, 1),
},
// Unsupported: we don't analyze `Cell` types
Cell<Option<ScopeId>>: { _ => Layout::known(4, 4, 0), },
Cell<Option<SymbolId>>: { _ => Layout::known(4, 4, 0), },
Cell<Option<ReferenceId>>: { _ => Layout::known(4, 4, 0), },
ReferenceFlag: { _ => Layout::known(1, 1, 0), },
AssignmentOperator: { _ => Layout::known(1, 1, 1), },
LogicalOperator: { _ => Layout::known(1, 1, 1), },
UnaryOperator: { _ => Layout::known(1, 1, 1), },
BinaryOperator: { _ => Layout::known(1, 1, 1), },
UpdateOperator: { _ => Layout::known(1, 1, 1), },
SourceType: { _ => Layout::known(4, 1, 1), },
// Unsupported: this is a `bitflags` generated type, we don't expand macros
ReferenceFlag: { _ => Layout::known(1, 1, 0), },
// Unsupported: this is a `bitflags` generated type, we don't expand macros
RegExpFlags: { _ => Layout::known(1, 1, 0), },
BigintBase: { _ => Layout::known(1, 1, 1), },
NumberBase: { _ => Layout::known(1, 1, 1), },
};
}

0 comments on commit 82e2f6b

Please sign in to comment.