From dcb38f22770de774c7762909094104636046a490 Mon Sep 17 00:00:00 2001 From: Boshen Date: Mon, 12 Jun 2023 20:55:16 +0800 Subject: [PATCH] refactor(semantic): move SymbolId and SymbolFlags to oxc_syntax --- Cargo.lock | 3 ++ crates/oxc_semantic/src/symbol/id.rs | 7 ---- crates/oxc_semantic/src/symbol/mod.rs | 43 +------------------ crates/oxc_semantic2/Cargo.toml | 5 ++- crates/oxc_semantic2/src/symbol.rs | 60 +-------------------------- crates/oxc_syntax/Cargo.toml | 2 + crates/oxc_syntax/src/lib.rs | 1 + crates/oxc_syntax/src/symbol.rs | 58 ++++++++++++++++++++++++++ 8 files changed, 70 insertions(+), 109 deletions(-) delete mode 100644 crates/oxc_semantic/src/symbol/id.rs create mode 100644 crates/oxc_syntax/src/symbol.rs diff --git a/Cargo.lock b/Cargo.lock index e3667da1f..9d3b52ac9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1371,6 +1371,7 @@ dependencies = [ "indexmap", "oxc_index", "oxc_span", + "oxc_syntax", "rustc-hash", ] @@ -1387,6 +1388,8 @@ dependencies = [ name = "oxc_syntax" version = "0.0.0" dependencies = [ + "bitflags 2.3.1", + "oxc_index", "serde", "unicode-id-start", ] diff --git a/crates/oxc_semantic/src/symbol/id.rs b/crates/oxc_semantic/src/symbol/id.rs deleted file mode 100644 index 79896ecdd..000000000 --- a/crates/oxc_semantic/src/symbol/id.rs +++ /dev/null @@ -1,7 +0,0 @@ -use std::hash::Hash; - -use oxc_index::define_index_type; - -define_index_type! { - pub struct SymbolId = u32; -} diff --git a/crates/oxc_semantic/src/symbol/mod.rs b/crates/oxc_semantic/src/symbol/mod.rs index 9a47386db..e1b47c763 100644 --- a/crates/oxc_semantic/src/symbol/mod.rs +++ b/crates/oxc_semantic/src/symbol/mod.rs @@ -1,19 +1,16 @@ //! Symbol and Symbol Table for tracking of semantics of variables -#![allow(non_upper_case_globals)] mod builder; -mod id; mod mangler; mod reference; mod table; -use bitflags::bitflags; use oxc_span::{Atom, Span}; +pub use oxc_syntax::symbol::{SymbolFlags, SymbolId}; use self::reference::ResolvedReferenceId; pub use self::{ builder::SymbolTableBuilder, - id::SymbolId, mangler::{Mangler, Slot}, reference::{Reference, ReferenceFlag, ResolvedReference}, table::SymbolTable, @@ -39,44 +36,6 @@ mod size_asserts { assert_eq_size!(super::Symbol, [u8; 88]); } -bitflags! { - #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] - pub struct SymbolFlags: u16 { - const None = 0; - /// Variable (var) or parameter - const FunctionScopedVariable = 1 << 0; - /// A block-scoped variable (let or const) - const BlockScopedVariable = 1 << 1; - /// A const variable (const) - const ConstVariable = 1 << 2; - /// Is this symbol inside an import declaration - const Import = 1 << 3; - /// Is this symbol inside an export declaration - const Export = 1 << 4; - const Class = 1 << 5; - const CatchVariable = 1 << 6; // try {} catch(catch_variable) {} - - const Variable = Self::FunctionScopedVariable.bits() | Self::BlockScopedVariable.bits(); - const Value = Self::Variable.bits() | Self::Class.bits(); - - /// Variables can be redeclared, but can not redeclare a block-scoped declaration with the - /// same name, or any other value that is not a variable, e.g. ValueModule or Class - const FunctionScopedVariableExcludes = Self::Value.bits() - Self::FunctionScopedVariable.bits(); - - /// Block-scoped declarations are not allowed to be re-declared - /// they can not merge with anything in the value space - const BlockScopedVariableExcludes = Self::Value.bits(); - - const ClassExcludes = Self::Value.bits(); - } -} - -impl SymbolFlags { - pub fn is_variable(&self) -> bool { - self.intersects(Self::Variable) - } -} - impl Symbol { pub fn new( id: SymbolId, diff --git a/crates/oxc_semantic2/Cargo.toml b/crates/oxc_semantic2/Cargo.toml index d3a8b88fd..959099bd6 100644 --- a/crates/oxc_semantic2/Cargo.toml +++ b/crates/oxc_semantic2/Cargo.toml @@ -10,8 +10,9 @@ license.workspace = true repository.workspace = true [dependencies] -oxc_span = { workspace = true } -oxc_index = { workspace = true } +oxc_span = { workspace = true } +oxc_index = { workspace = true } +oxc_syntax = { workspace = true } bitflags = { workspace = true } rustc-hash = { workspace = true } diff --git a/crates/oxc_semantic2/src/symbol.rs b/crates/oxc_semantic2/src/symbol.rs index 9156a6ec8..d1b47f170 100644 --- a/crates/oxc_semantic2/src/symbol.rs +++ b/crates/oxc_semantic2/src/symbol.rs @@ -1,68 +1,12 @@ -#![allow(non_upper_case_globals)] - -use bitflags::bitflags; -use oxc_index::{define_index_type, IndexVec}; +use oxc_index::IndexVec; use oxc_span::{Atom, Span}; +pub use oxc_syntax::symbol::{SymbolFlags, SymbolId}; use crate::{ reference::{Reference, ReferenceId}, scope::ScopeId, }; -define_index_type! { - pub struct SymbolId = u32; -} - -bitflags! { - #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] - pub struct SymbolFlags: u16 { - /// Variable (var) or parameter - const FunctionScopedVariable = 1 << 0; - /// A block-scoped variable (let or const) - const BlockScopedVariable = 1 << 1; - /// A const variable (const) - const ConstVariable = 1 << 2; - /// Is this symbol inside an import declaration - const Import = 1 << 3; - /// Is this symbol inside an export declaration - const Export = 1 << 4; - const Class = 1 << 5; - const CatchVariable = 1 << 6; // try {} catch(catch_variable) {} - const Function = 1 << 7; - - const Variable = Self::FunctionScopedVariable.bits() | Self::BlockScopedVariable.bits(); - const Value = Self::Variable.bits() | Self::Class.bits(); - - /// Variables can be redeclared, but can not redeclare a block-scoped declaration with the - /// same name, or any other value that is not a variable, e.g. ValueModule or Class - const FunctionScopedVariableExcludes = Self::Value.bits() - Self::FunctionScopedVariable.bits(); - - /// Block-scoped declarations are not allowed to be re-declared - /// they can not merge with anything in the value space - const BlockScopedVariableExcludes = Self::Value.bits(); - - const ClassExcludes = Self::Value.bits(); - } -} - -impl SymbolFlags { - pub fn is_variable(&self) -> bool { - self.intersects(Self::Variable) - } - - pub fn is_function(&self) -> bool { - self.contains(Self::Function) - } - - pub fn is_catch_variable(&self) -> bool { - self.contains(Self::CatchVariable) - } - - pub fn is_function_scoped_declaration(&self) -> bool { - self.contains(Self::FunctionScopedVariable) - } -} - /// Symbol Table /// /// `SoA` (Struct of Arrays) for memory efficiency. diff --git a/crates/oxc_syntax/Cargo.toml b/crates/oxc_syntax/Cargo.toml index e71fd4cf0..3969940da 100644 --- a/crates/oxc_syntax/Cargo.toml +++ b/crates/oxc_syntax/Cargo.toml @@ -17,3 +17,5 @@ serde = ["dep:serde"] [dependencies] unicode-id-start = { workspace = true } serde = { workspace = true, features = ["derive"], optional = true } +oxc_index = { workspace = true } +bitflags = { workspace = true } diff --git a/crates/oxc_syntax/src/lib.rs b/crates/oxc_syntax/src/lib.rs index 6545a187d..796b1692b 100644 --- a/crates/oxc_syntax/src/lib.rs +++ b/crates/oxc_syntax/src/lib.rs @@ -3,6 +3,7 @@ pub mod identifier; pub mod operator; pub mod precedence; +pub mod symbol; pub use unicode_id_start; diff --git a/crates/oxc_syntax/src/symbol.rs b/crates/oxc_syntax/src/symbol.rs new file mode 100644 index 000000000..c4bc80427 --- /dev/null +++ b/crates/oxc_syntax/src/symbol.rs @@ -0,0 +1,58 @@ +#![allow(non_upper_case_globals)] + +use bitflags::bitflags; +use oxc_index::define_index_type; + +define_index_type! { + pub struct SymbolId = u32; +} + +bitflags! { + #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] + pub struct SymbolFlags: u16 { + /// Variable (var) or parameter + const FunctionScopedVariable = 1 << 0; + /// A block-scoped variable (let or const) + const BlockScopedVariable = 1 << 1; + /// A const variable (const) + const ConstVariable = 1 << 2; + /// Is this symbol inside an import declaration + const Import = 1 << 3; + /// Is this symbol inside an export declaration + const Export = 1 << 4; + const Class = 1 << 5; + const CatchVariable = 1 << 6; // try {} catch(catch_variable) {} + const Function = 1 << 7; + + const Variable = Self::FunctionScopedVariable.bits() | Self::BlockScopedVariable.bits(); + const Value = Self::Variable.bits() | Self::Class.bits(); + + /// Variables can be redeclared, but can not redeclare a block-scoped declaration with the + /// same name, or any other value that is not a variable, e.g. ValueModule or Class + const FunctionScopedVariableExcludes = Self::Value.bits() - Self::FunctionScopedVariable.bits(); + + /// Block-scoped declarations are not allowed to be re-declared + /// they can not merge with anything in the value space + const BlockScopedVariableExcludes = Self::Value.bits(); + + const ClassExcludes = Self::Value.bits(); + } +} + +impl SymbolFlags { + pub fn is_variable(&self) -> bool { + self.intersects(Self::Variable) + } + + pub fn is_function(&self) -> bool { + self.contains(Self::Function) + } + + pub fn is_catch_variable(&self) -> bool { + self.contains(Self::CatchVariable) + } + + pub fn is_function_scoped_declaration(&self) -> bool { + self.contains(Self::FunctionScopedVariable) + } +}