Skip to main content

oxc_semantic/node/
mod.rs

1mod nodes;
2
3pub use nodes::AstNodes;
4
5use oxc_allocator::{Address, GetAddress};
6use oxc_ast::AstKind;
7use oxc_span::{GetSpan, Span};
8use oxc_syntax::{node::NodeId, scope::ScopeId};
9
10/// Semantic node contains all the semantic information about an ast node.
11#[derive(Debug, Clone, Copy)]
12pub struct AstNode<'a> {
13    /// A pointer to the ast node, which resides in the memory arena.
14    kind: AstKind<'a>,
15
16    /// Associated Scope (initialized by binding)
17    scope_id: ScopeId,
18}
19
20impl<'a> AstNode<'a> {
21    pub(crate) fn new(kind: AstKind<'a>, scope_id: ScopeId) -> Self {
22        Self { kind, scope_id }
23    }
24
25    /// Access the underlying struct from [`oxc_ast`].
26    #[inline]
27    pub fn kind(&self) -> AstKind<'a> {
28        self.kind
29    }
30
31    /// Node id assigned to this AST node.
32    #[inline]
33    pub fn id(&self) -> NodeId {
34        self.kind.node_id()
35    }
36
37    /// The scope in which this node was declared.
38    ///
39    /// It is important to note that this is _not_ the scope created _by_ the
40    /// node. For example, given a function declaration, this is the scope where
41    /// the function is declared, not the scope created by its body.
42    #[inline]
43    pub fn scope_id(&self) -> ScopeId {
44        self.scope_id
45    }
46}
47
48impl GetSpan for AstNode<'_> {
49    #[inline]
50    fn span(&self) -> Span {
51        self.kind.span()
52    }
53}
54
55impl GetAddress for AstNode<'_> {
56    #[inline]
57    fn address(&self) -> Address {
58        self.kind.address()
59    }
60}