mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(semantic): move SymbolId and SymbolFlags to oxc_syntax
This commit is contained in:
parent
a518bcbb3b
commit
dcb38f2277
8 changed files with 70 additions and 109 deletions
3
Cargo.lock
generated
3
Cargo.lock
generated
|
|
@ -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",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
use std::hash::Hash;
|
||||
|
||||
use oxc_index::define_index_type;
|
||||
|
||||
define_index_type! {
|
||||
pub struct SymbolId = u32;
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
pub mod identifier;
|
||||
pub mod operator;
|
||||
pub mod precedence;
|
||||
pub mod symbol;
|
||||
|
||||
pub use unicode_id_start;
|
||||
|
||||
|
|
|
|||
58
crates/oxc_syntax/src/symbol.rs
Normal file
58
crates/oxc_syntax/src/symbol.rs
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue