diff --git a/crates/oxc_semantic/src/node/mod.rs b/crates/oxc_semantic/src/node/mod.rs new file mode 100644 index 0000000000000..770d015183f21 --- /dev/null +++ b/crates/oxc_semantic/src/node/mod.rs @@ -0,0 +1,75 @@ +mod nodes; + +pub use nodes::AstNodes; + +use oxc_allocator::{Address, GetAddress}; +use oxc_ast::AstKind; +#[cfg(feature = "cfg")] +use oxc_cfg::BlockNodeId; +use oxc_span::{GetSpan, Span}; +use oxc_syntax::{node::NodeId, scope::ScopeId}; + +/// Semantic node contains all the semantic information about an ast node. +#[derive(Debug, Clone, Copy)] +pub struct AstNode<'a> { + id: NodeId, + /// A pointer to the ast node, which resides in the `bumpalo` memory arena. + kind: AstKind<'a>, + + /// Associated Scope (initialized by binding) + scope_id: ScopeId, +} + +impl<'a> AstNode<'a> { + #[inline] + #[cfg(feature = "cfg")] + pub(crate) fn new( + kind: AstKind<'a>, + scope_id: ScopeId, + _cfg_id: BlockNodeId, + id: NodeId, + ) -> Self { + Self { id, kind, scope_id } + } + + #[cfg(not(feature = "cfg"))] + pub(crate) fn new(kind: AstKind<'a>, scope_id: ScopeId, _cfg_id: (), id: NodeId) -> Self { + Self { id, kind, scope_id } + } + + /// This node's unique identifier. + #[inline] + pub fn id(&self) -> NodeId { + self.id + } + + /// Access the underlying struct from [`oxc_ast`]. + #[inline] + pub fn kind(&self) -> AstKind<'a> { + self.kind + } + + /// The scope in which this node was declared. + /// + /// It is important to note that this is _not_ the scope created _by_ the + /// node. For example, given a function declaration, this is the scope where + /// the function is declared, not the scope created by its body. + #[inline] + pub fn scope_id(&self) -> ScopeId { + self.scope_id + } +} + +impl GetSpan for AstNode<'_> { + #[inline] + fn span(&self) -> Span { + self.kind.span() + } +} + +impl GetAddress for AstNode<'_> { + #[inline] + fn address(&self) -> Address { + self.kind.address() + } +} diff --git a/crates/oxc_semantic/src/node.rs b/crates/oxc_semantic/src/node/nodes.rs similarity index 86% rename from crates/oxc_semantic/src/node.rs rename to crates/oxc_semantic/src/node/nodes.rs index 816e43e35b895..44880e37eb96a 100644 --- a/crates/oxc_semantic/src/node.rs +++ b/crates/oxc_semantic/src/node/nodes.rs @@ -1,83 +1,17 @@ use std::iter::FusedIterator; -use oxc_allocator::{Address, GetAddress}; use oxc_ast::{AstKind, AstType, ast::Program}; #[cfg(feature = "cfg")] use oxc_cfg::BlockNodeId; use oxc_index::{IndexSlice, IndexVec}; -use oxc_span::{GetSpan, Span}; use oxc_syntax::{ node::{NodeFlags, NodeId}, scope::ScopeId, }; +use super::AstNode; use crate::ast_types_bitset::AstTypesBitset; -/// Semantic node contains all the semantic information about an ast node. -#[derive(Debug, Clone, Copy)] -pub struct AstNode<'a> { - id: NodeId, - /// A pointer to the ast node, which resides in the `bumpalo` memory arena. - kind: AstKind<'a>, - - /// Associated Scope (initialized by binding) - scope_id: ScopeId, -} - -impl<'a> AstNode<'a> { - #[inline] - #[cfg(feature = "cfg")] - pub(crate) fn new( - kind: AstKind<'a>, - scope_id: ScopeId, - _cfg_id: BlockNodeId, - id: NodeId, - ) -> Self { - Self { id, kind, scope_id } - } - - #[cfg(not(feature = "cfg"))] - pub(crate) fn new(kind: AstKind<'a>, scope_id: ScopeId, _cfg_id: (), id: NodeId) -> Self { - Self { id, kind, scope_id } - } - - /// This node's unique identifier. - #[inline] - pub fn id(&self) -> NodeId { - self.id - } - - /// Access the underlying struct from [`oxc_ast`]. - #[inline] - pub fn kind(&self) -> AstKind<'a> { - self.kind - } - - /// The scope in which this node was declared. - /// - /// It is important to note that this is _not_ the scope created _by_ the - /// node. For example, given a function declaration, this is the scope where - /// the function is declared, not the scope created by its body. - #[inline] - pub fn scope_id(&self) -> ScopeId { - self.scope_id - } -} - -impl GetSpan for AstNode<'_> { - #[inline] - fn span(&self) -> Span { - self.kind.span() - } -} - -impl GetAddress for AstNode<'_> { - #[inline] - fn address(&self) -> Address { - self.kind.address() - } -} - /// Untyped AST nodes flattened into an vec #[derive(Debug, Default)] pub struct AstNodes<'a> { @@ -146,7 +80,7 @@ impl<'a> AstNodes<'a> { /// Access the underlying struct from [`oxc_ast`]. #[inline] pub fn kind(&self, node_id: NodeId) -> AstKind<'a> { - self.nodes[node_id].kind + self.nodes[node_id].kind() } /// Get id of this node's parent. @@ -200,7 +134,7 @@ impl<'a> AstNodes<'a> { #[inline] pub fn program(&self) -> &'a Program<'a> { if let Some(node) = self.nodes.first() - && let AstKind::Program(program) = node.kind + && let AstKind::Program(program) = node.kind() { return program; }