Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions crates/oxc_semantic/src/node/mod.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down
Loading