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
use std::{
    fmt,
    sync::atomic::{AtomicU32, Ordering::SeqCst},
};

use swc_atoms::JsWord;
use swc_common::{collections::AHashMap, sync::Lock, FileName, Mark, SyntaxContext, DUMMY_SP};
use swc_ecma_ast::{Expr, Ident};
use swc_ecma_utils::ident::IdentLike;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ModuleId(u32);

impl fmt::Display for ModuleId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.0, f)
    }
}

impl fmt::Debug for ModuleId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "ModuleId({})", self.0)?;
        Ok(())
    }
}

#[derive(Debug, Default)]
pub(crate) struct ModuleIdGenerator {
    v: AtomicU32,
    /// `(module_id, local_mark, export_mark)`
    cache: Lock<AHashMap<FileName, (ModuleId, Mark, Mark)>>,
}

impl ModuleIdGenerator {
    pub fn gen(&self, file_name: &FileName) -> (ModuleId, Mark, Mark) {
        let mut w = self.cache.lock();
        if let Some(v) = w.get(file_name) {
            return *v;
        }

        let id = ModuleId(self.v.fetch_add(1, SeqCst));
        let local_mark = Mark::fresh(Mark::root());
        let export_mark = Mark::fresh(Mark::root());
        let v = (id, local_mark, export_mark);
        w.insert(file_name.clone(), v);
        (id, local_mark, export_mark)
    }
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id(JsWord, SyntaxContext);

impl fmt::Debug for Id {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}{:?}", self.0, self.1)
    }
}

impl Id {
    pub fn new(sym: JsWord, ctxt: SyntaxContext) -> Self {
        Id(sym, ctxt)
    }

    pub fn sym(&self) -> &JsWord {
        &self.0
    }

    pub fn ctxt(&self) -> SyntaxContext {
        self.1
    }

    pub fn into_ident(self) -> Ident {
        Ident::new(self.0, DUMMY_SP.with_ctxt(self.1))
    }

    pub fn with_ctxt(mut self, ctxt: SyntaxContext) -> Self {
        self.1 = ctxt;
        self
    }
}

impl IdentLike for Id {
    fn from_ident(i: &Ident) -> Self {
        i.into()
    }

    fn to_id(&self) -> (JsWord, SyntaxContext) {
        (self.0.clone(), self.1)
    }

    fn into_id(self) -> (JsWord, SyntaxContext) {
        (self.0, self.1)
    }
}

impl From<Ident> for Id {
    fn from(i: Ident) -> Self {
        Id(i.sym, i.span.ctxt())
    }
}

impl<'a> From<&'a Ident> for Id {
    fn from(i: &Ident) -> Self {
        Id(i.sym.clone(), i.span.ctxt())
    }
}

impl PartialEq<Ident> for Id {
    fn eq(&self, other: &Ident) -> bool {
        self.0 == other.sym && self.1 == other.span.ctxt()
    }
}

impl PartialEq<JsWord> for Id {
    fn eq(&self, other: &JsWord) -> bool {
        self.0 == *other
    }
}

impl From<Id> for Ident {
    #[inline]
    fn from(id: Id) -> Self {
        id.into_ident()
    }
}

impl From<Id> for Expr {
    #[inline]
    fn from(id: Id) -> Self {
        Expr::Ident(id.into_ident())
    }
}