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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use std::borrow::Borrow;
use std::fmt;

#[allow(unused_imports)]
use erg_common::log;
use erg_common::set::Set;
use erg_common::{switch_lang, Str};

use erg_parser::ast::AccessModifier;

use crate::context::Context;
use crate::ty::Type;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum VisibilityModifier {
    Public,
    Private,
    Restricted(Set<Str>),
    SubtypeRestricted(Type),
}

impl fmt::Display for VisibilityModifier {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Private => write!(f, "::"),
            Self::Public => write!(f, "."),
            Self::Restricted(namespaces) => write!(f, "::[{namespaces}]"),
            Self::SubtypeRestricted(typ) => write!(f, "::[<: {typ}]"),
        }
    }
}

impl VisibilityModifier {
    pub const fn is_public(&self) -> bool {
        matches!(self, Self::Public)
    }
    pub const fn is_private(&self) -> bool {
        matches!(self, Self::Private)
    }

    pub const fn display_as_accessor(&self) -> &'static str {
        match self {
            Self::Public => ".",
            Self::Private | Self::Restricted(_) | Self::SubtypeRestricted(_) => "::",
        }
    }

    pub fn display(&self) -> String {
        match self {
            Self::Private => switch_lang!(
                "japanese" => "非公開",
                "simplified_chinese" => "私有",
                "traditional_chinese" => "私有",
                "english" => "private",
            )
            .into(),
            Self::Public => switch_lang!(
                "japanese" => "公開",
                "simplified_chinese" => "公开",
                "traditional_chinese" => "公開",
                "english" => "public",
            )
            .into(),
            Self::Restricted(namespaces) => switch_lang!(
                "japanese" => format!("制限付き公開({namespaces}でのみ公開)"),
                "simplified_chinese" => format!("受限公开({namespaces}中可见)"),
                "traditional_chinese" => format!("受限公開({namespaces}中可見)"),
                "english" => format!("restricted public ({namespaces} only)"),
            ),
            Self::SubtypeRestricted(typ) => switch_lang!(
                "japanese" => format!("制限付き公開({typ}の部分型でのみ公開)"),
                "simplified_chinese" => format!("受限公开({typ}的子类型中可见)"),
                "traditional_chinese" => format!("受限公開({typ}的子類型中可見)"),
                "english" => format!("restricted public (subtypes of {typ} only)"),
            ),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Visibility {
    pub modifier: VisibilityModifier,
    pub def_namespace: Str,
}

impl Visibility {
    pub const DUMMY_PRIVATE: Self = Self {
        modifier: VisibilityModifier::Private,
        def_namespace: Str::ever("<dummy>"),
    };
    pub const DUMMY_PUBLIC: Self = Self {
        modifier: VisibilityModifier::Public,
        def_namespace: Str::ever("<dummy>"),
    };
    pub const BUILTIN_PRIVATE: Self = Self {
        modifier: VisibilityModifier::Private,
        def_namespace: Str::ever("<builtins>"),
    };
    pub const BUILTIN_PUBLIC: Self = Self {
        modifier: VisibilityModifier::Public,
        def_namespace: Str::ever("<builtins>"),
    };

    pub const fn new(modifier: VisibilityModifier, def_namespace: Str) -> Self {
        Self {
            modifier,
            def_namespace,
        }
    }

    pub fn private<S: Into<Str>>(namespace: S) -> Self {
        Self {
            modifier: VisibilityModifier::Private,
            def_namespace: namespace.into(),
        }
    }

    pub const fn is_public(&self) -> bool {
        self.modifier.is_public()
    }
    pub const fn is_private(&self) -> bool {
        self.modifier.is_private()
    }

    pub fn compatible(&self, access: &AccessModifier, namespace: &Context) -> bool {
        match (&self.modifier, access) {
            (_, AccessModifier::Force) => true,
            (VisibilityModifier::Public, AccessModifier::Auto | AccessModifier::Public) => true,
            // compatible example:
            //   def_namespace: <module>::C
            //   namespace: <module>::C::f
            (VisibilityModifier::Private, AccessModifier::Auto | AccessModifier::Private) => {
                &self.def_namespace[..] == "<builtins>"
                    || namespace.name.starts_with(&self.def_namespace[..])
            }
            (
                VisibilityModifier::Restricted(namespaces),
                AccessModifier::Auto | AccessModifier::Private,
            ) => {
                namespace.name.starts_with(&self.def_namespace[..])
                    || namespaces.contains(&namespace.name)
            }
            (
                VisibilityModifier::SubtypeRestricted(typ),
                AccessModifier::Auto | AccessModifier::Private,
            ) => {
                namespace.name.starts_with(&self.def_namespace[..]) || {
                    let Some(space_t) = namespace.rec_get_self_t() else {
                        return false;
                    };
                    namespace.subtype_of(&space_t, typ)
                }
            }
            _ => false,
        }
    }
}

/// same structure as `Identifier`, but only for Record fields.
#[derive(Debug, Clone, Eq)]
pub struct Field {
    pub vis: VisibilityModifier,
    pub symbol: Str,
}

impl PartialEq for Field {
    fn eq(&self, other: &Self) -> bool {
        self.symbol == other.symbol
    }
}

impl std::hash::Hash for Field {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.symbol.hash(state);
    }
}

impl fmt::Display for Field {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}{}", self.vis, self.symbol)
    }
}

impl Borrow<str> for Field {
    #[inline]
    fn borrow(&self) -> &str {
        &self.symbol[..]
    }
}

impl Borrow<Str> for Field {
    #[inline]
    fn borrow(&self) -> &Str {
        &self.symbol
    }
}

impl Field {
    pub const fn new(vis: VisibilityModifier, symbol: Str) -> Self {
        Field { vis, symbol }
    }

    pub fn private(symbol: Str) -> Self {
        Field::new(VisibilityModifier::Private, symbol)
    }

    pub fn public(symbol: Str) -> Self {
        Field::new(VisibilityModifier::Public, symbol)
    }

    pub fn is_const(&self) -> bool {
        self.symbol.starts_with(char::is_uppercase)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use erg_common::dict;

    #[test]
    fn test_std_key() {
        let dict = dict! {Str::ever("a") => 1, Str::rc("b") => 2};
        assert_eq!(dict.get("a"), Some(&1));
        assert_eq!(dict.get("b"), Some(&2));
        assert_eq!(dict.get(&Str::ever("b")), Some(&2));
        assert_eq!(dict.get(&Str::rc("b")), Some(&2));

        let dict = dict! {Field::private(Str::ever("a")) => 1, Field::public(Str::ever("b")) => 2};
        assert_eq!(dict.get("a"), Some(&1));
        assert_eq!(dict.get("b"), Some(&2));
    }
}