1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! RNode: Node with node id

use std::sync::Arc;

pub use rnode_macros::define_rnode;
use serde::{Deserialize, Serialize};
use stc_visit::Visitable;
pub use stc_visit::{Fold, FoldWith, Visit, VisitMut, VisitMutWith, VisitWith};
use swc_common::{EqIgnoreSpan, TypeEq};

/// Alternative for span. This is much more reliable than span.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct NodeId(u32);

/// Always returns `true` as the struct is an alternative for span.
impl EqIgnoreSpan for NodeId {
    #[inline]
    fn eq_ignore_span(&self, _: &Self) -> bool {
        true
    }
}

/// Always returns `true` as id of a node is not type.
impl TypeEq for NodeId {
    #[inline]
    fn type_eq(&self, _: &Self) -> bool {
        true
    }
}

impl NodeId {
    pub const fn is_invalid(self) -> bool {
        self.0 == 0
    }

    pub const fn invalid() -> Self {
        Self(0)
    }
}

impl Visitable for NodeId {}

/// Noop.
impl<V: ?Sized> VisitWith<V> for NodeId {
    fn visit_children_with(&self, _: &mut V) {}
}

/// Noop.
impl<V: ?Sized> VisitMutWith<V> for NodeId {
    fn visit_mut_children_with(&mut self, _: &mut V) {}
}

/// Noop.
impl<V: ?Sized> FoldWith<V> for NodeId {
    fn fold_children_with(self, _: &mut V) -> Self {
        self
    }
}

#[derive(Debug)]
pub struct NodeIdGenerator {
    /// If the stored value is zero, it's an invalid id generator.
    inner: u32,
}

impl Default for NodeIdGenerator {
    fn default() -> Self {
        Self { inner: 1 }
    }
}

impl NodeIdGenerator {
    pub fn invalid() -> Self {
        Self { inner: 0 }
    }

    pub fn make<R>(&mut self, orig: R::Orig) -> R
    where
        R: RNode,
    {
        R::from_orig(self, orig)
    }

    pub fn gen(&mut self) -> NodeId {
        let v = self.inner;
        if v == 0 {
            return NodeId::invalid();
        }
        self.inner += 1;
        NodeId(v)
    }
}

pub trait RNode: Clone + Send + Sync {
    type Orig;

    fn from_orig(id_generator: &mut NodeIdGenerator, orig: Self::Orig) -> Self;

    fn into_orig(self) -> Self::Orig;
}

impl<R> RNode for Box<R>
where
    R: RNode,
{
    type Orig = <R as RNode>::Orig;

    fn from_orig(g: &mut NodeIdGenerator, orig: Self::Orig) -> Self {
        Box::new(R::from_orig(g, orig))
    }

    fn into_orig(self) -> Self::Orig {
        (*self).into_orig()
    }
}

impl<R> RNode for Vec<R>
where
    R: RNode,
{
    type Orig = Vec<<R as RNode>::Orig>;

    fn from_orig(g: &mut NodeIdGenerator, orig: Self::Orig) -> Self {
        orig.into_iter().map(|orig| R::from_orig(g, orig)).collect()
    }

    fn into_orig(self) -> Self::Orig {
        self.into_iter().map(R::into_orig).collect()
    }
}

impl<R> RNode for Option<R>
where
    R: RNode,
{
    type Orig = Option<R::Orig>;

    fn from_orig(g: &mut NodeIdGenerator, orig: Self::Orig) -> Self {
        orig.map(|v| R::from_orig(g, v))
    }

    fn into_orig(self) -> Self::Orig {
        self.map(|v| v.into_orig())
    }
}

/// Helper for derive macro. Do **not** implement this manually.
pub trait IntoRNode<R> {
    fn into_rnode(self, g: &mut NodeIdGenerator) -> R;
}

impl<R, N> IntoRNode<R> for N
where
    R: RNode<Orig = Self>,
{
    fn into_rnode(self, g: &mut NodeIdGenerator) -> R {
        RNode::from_orig(g, self)
    }
}

impl<T> RNode for Arc<T>
where
    T: RNode,
{
    type Orig = <T as RNode>::Orig;

    fn from_orig(id_generator: &mut NodeIdGenerator, orig: Self::Orig) -> Self {
        let t = T::from_orig(id_generator, orig);
        Arc::new(t)
    }

    fn into_orig(self) -> Self::Orig {
        match Arc::try_unwrap(self) {
            Ok(v) => v.into_orig(),
            Err(err) => (*err).clone().into_orig(),
        }
    }
}