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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use std::fmt;
use std::path::Path;

use erg_common::error::Location;
use erg_common::pathutil::NormalizedPathBuf;
use erg_common::set::Set;
use erg_common::{switch_lang, Str};

use erg_parser::ast::DefId;

use crate::context::{ContextKind, DefaultInfo};
use crate::ty::{Field, HasType, Type, Visibility};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum Mutability {
    Immutable,
    Const,
}

impl From<&str> for Mutability {
    fn from(item: &str) -> Self {
        if item.chars().next().is_some_and(|c| c.is_uppercase()) {
            Self::Const
        } else {
            Self::Immutable
        }
    }
}

impl Mutability {
    pub const fn is_const(&self) -> bool {
        matches!(self, Self::Const)
    }
}

use Mutability::*;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum VarKind {
    Defined(DefId),
    Declared,
    InstanceAttr,
    Parameter {
        def_id: DefId,
        var: bool,
        default: DefaultInfo,
    },
    Auto,
    FixedAuto,
    DoesNotExist,
    Builtin,
}

impl VarKind {
    pub const fn parameter(def_id: DefId, var: bool, default: DefaultInfo) -> Self {
        Self::Parameter {
            def_id,
            var,
            default,
        }
    }

    pub const fn nd_parameter(def_id: DefId) -> Self {
        Self::parameter(def_id, false, DefaultInfo::NonDefault)
    }

    pub const fn has_default(&self) -> bool {
        match self {
            Self::Parameter { default, .. } => default.has_default(),
            _ => false,
        }
    }

    pub const fn is_parameter(&self) -> bool {
        matches!(self, Self::Parameter { .. })
    }

    pub const fn is_var_params(&self) -> bool {
        match self {
            Self::Parameter { var, .. } => *var,
            _ => false,
        }
    }

    pub const fn is_defined(&self) -> bool {
        matches!(self, Self::Defined(_))
    }

    pub const fn can_capture(&self) -> bool {
        matches!(
            self,
            Self::Defined(_) | Self::Declared | Self::Parameter { .. }
        )
    }

    pub const fn does_not_exist(&self) -> bool {
        matches!(self, Self::DoesNotExist)
    }

    pub const fn is_builtin(&self) -> bool {
        matches!(self, Self::Builtin)
    }

    pub const fn is_auto(&self) -> bool {
        matches!(self, Self::Auto)
    }

    pub const fn is_instance_attr(&self) -> bool {
        matches!(self, Self::InstanceAttr)
    }

    pub const fn display(&self) -> &'static str {
        match self {
            Self::Auto | Self::FixedAuto => switch_lang!(
                "japanese" => "自動",
                "simplified_chinese" => "自动",
                "traditional_chinese" => "自動",
                "english" => "auto",
            ),
            Self::Builtin => switch_lang!(
                "japanese" => "組み込み",
                "simplified_chinese" => "内置",
                "traditional_chinese" => "內置",
                "english" => "builtin",
            ),
            Self::InstanceAttr => switch_lang!(
                "japanese" => "インスタンス",
                "simplified_chinese" => "实例",
                "traditional_chinese" => "實例",
                "english" => "instance",
            ),
            _ => "",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AbsLocation {
    pub module: Option<NormalizedPathBuf>,
    pub loc: Location,
}

impl fmt::Display for AbsLocation {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(module) = &self.module {
            write!(f, "{}@{}", module.display(), self.loc)
        } else {
            write!(f, "?@{}", self.loc)
        }
    }
}

impl std::str::FromStr for AbsLocation {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut split = s.split('@');
        let module = split.next().map(NormalizedPathBuf::from);
        let loc = split.next().ok_or(())?.parse().map_err(|_| ())?;
        Ok(Self { module, loc })
    }
}

impl AbsLocation {
    pub const fn new(module: Option<NormalizedPathBuf>, loc: Location) -> Self {
        Self { module, loc }
    }

    pub const fn unknown() -> Self {
        Self::new(None, Location::Unknown)
    }

    pub fn is_unknown(&self) -> bool {
        self.module.is_none() && self.loc.is_unknown()
    }

    pub fn is_real(&self) -> bool {
        self.module.is_some() && self.loc.is_real()
    }

    pub fn starts_with(&self, path: impl AsRef<Path>) -> bool {
        self.module.as_ref().is_some_and(|p| p.starts_with(path))
    }

    pub fn code(&self) -> Option<String> {
        use std::io::{BufRead, BufReader};
        self.module.as_ref().and_then(|module| {
            let file = std::fs::File::open(module).ok()?;
            let reader = BufReader::new(file);
            reader
                .lines()
                .nth(
                    self.loc
                        .ln_begin()
                        .map(|l| l.saturating_sub(1))
                        .unwrap_or(0) as usize,
                )
                .and_then(|res| {
                    let res = res.ok()?;
                    let begin = self.loc.col_begin().unwrap_or(0) as usize;
                    let end = self.loc.col_end().unwrap_or(0) as usize;
                    if begin > res.len() || end > res.len() || begin > end {
                        return None;
                    }
                    let end = end.min(res.len());
                    let res = res[begin..end].to_string();
                    Some(res)
                })
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AliasInfo {
    pub name: Str,
    pub loc: AbsLocation,
}

impl AliasInfo {
    pub const fn new(name: Str, loc: AbsLocation) -> Self {
        Self { name, loc }
    }
}

/// Has information about the type, variability, visibility, and where the variable was defined (or declared, generated)
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VarInfo {
    pub t: Type,
    pub muty: Mutability,
    pub vis: Visibility,
    pub kind: VarKind,
    pub comptime_decos: Option<Set<Str>>,
    pub ctx: ContextKind,
    pub py_name: Option<Str>,
    pub def_loc: AbsLocation,
    pub alias_of: Option<AliasInfo>,
}

impl fmt::Display for VarInfo {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "VarInfo{{t: {}, muty: {:?}, vis: {:?}, kind: {:?}, py_name: {:?}, def_loc: {} }}",
            self.t, self.muty, self.vis, self.kind, self.py_name, self.def_loc
        )
    }
}

impl HasType for VarInfo {
    #[inline]
    fn ref_t(&self) -> &Type {
        &self.t
    }
    #[inline]
    fn ref_mut_t(&mut self) -> Option<&mut Type> {
        Some(&mut self.t)
    }
    #[inline]
    fn signature_t(&self) -> Option<&Type> {
        None
    }
    #[inline]
    fn signature_mut_t(&mut self) -> Option<&mut Type> {
        None
    }
}

impl Default for VarInfo {
    fn default() -> Self {
        Self::const_default_private()
    }
}

impl VarInfo {
    pub const ILLEGAL: Self = Self::const_default_private();

    pub const fn const_default_private() -> Self {
        Self::new(
            Type::Failure,
            Immutable,
            Visibility::DUMMY_PRIVATE,
            VarKind::DoesNotExist,
            None,
            ContextKind::Dummy,
            None,
            AbsLocation::unknown(),
        )
    }

    pub const fn const_default_public() -> Self {
        Self::new(
            Type::Failure,
            Immutable,
            Visibility::DUMMY_PUBLIC,
            VarKind::DoesNotExist,
            None,
            ContextKind::Dummy,
            None,
            AbsLocation::unknown(),
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub const fn new(
        t: Type,
        muty: Mutability,
        vis: Visibility,
        kind: VarKind,
        comptime_decos: Option<Set<Str>>,
        ctx: ContextKind,
        py_name: Option<Str>,
        def_loc: AbsLocation,
    ) -> Self {
        Self {
            t,
            muty,
            vis,
            kind,
            comptime_decos,
            ctx,
            py_name,
            def_loc,
            alias_of: None,
        }
    }

    #[allow(clippy::too_many_arguments)]
    pub const fn maybe_alias(
        t: Type,
        muty: Mutability,
        vis: Visibility,
        kind: VarKind,
        comptime_decos: Option<Set<Str>>,
        ctx: ContextKind,
        py_name: Option<Str>,
        def_loc: AbsLocation,
        alias_of: Option<AliasInfo>,
    ) -> Self {
        Self {
            t,
            muty,
            vis,
            kind,
            comptime_decos,
            ctx,
            py_name,
            def_loc,
            alias_of,
        }
    }

    pub fn same_id_as(&self, id: DefId) -> bool {
        match self.kind {
            VarKind::Defined(i) | VarKind::Parameter { def_id: i, .. } => id == i,
            _ => false,
        }
    }

    pub fn nd_parameter(t: Type, def_loc: AbsLocation, namespace: Str) -> Self {
        let kind = VarKind::Parameter {
            def_id: DefId(0),
            var: false,
            default: DefaultInfo::NonDefault,
        };
        Self::new(
            t,
            Immutable,
            Visibility::private(namespace),
            kind,
            None,
            ContextKind::Func,
            None,
            def_loc,
        )
    }

    pub fn d_parameter(t: Type, def_loc: AbsLocation, namespace: Str) -> Self {
        let kind = VarKind::Parameter {
            def_id: DefId(0),
            var: false,
            default: DefaultInfo::WithDefault,
        };
        Self::new(
            t,
            Immutable,
            Visibility::private(namespace),
            kind,
            None,
            ContextKind::Func,
            None,
            def_loc,
        )
    }

    pub fn instance_attr(
        field: Field,
        t: Type,
        kind: ContextKind,
        namespace: Str,
        loc: AbsLocation,
    ) -> Self {
        let muty = if field.is_const() {
            Mutability::Const
        } else {
            Mutability::Immutable
        };
        Self::new(
            t,
            muty,
            Visibility::new(field.vis, namespace),
            VarKind::InstanceAttr,
            None,
            kind,
            None,
            loc,
        )
    }

    pub fn type_var(t: Type, def_loc: AbsLocation, namespace: Str) -> Self {
        Self::new(
            t,
            Const,
            Visibility::private(namespace),
            VarKind::Declared,
            None,
            ContextKind::Dummy,
            None,
            def_loc,
        )
    }

    pub fn record_field(t: Type, def_loc: AbsLocation, vis: Visibility) -> Self {
        Self::new(
            t,
            Immutable,
            vis,
            VarKind::Declared,
            None,
            ContextKind::Dummy,
            None,
            def_loc,
        )
    }

    pub fn is_ambiguously_typed_parameter(&self) -> bool {
        self.kind.is_parameter()
            && self
                .t
                .get_super()
                .is_some_and(|sup| sup == Type::Obj || sup.is_structural())
    }

    pub const fn is_parameter(&self) -> bool {
        self.kind.is_parameter()
    }

    pub fn impl_of(&self) -> Option<&Type> {
        match &self.ctx {
            ContextKind::MethodDefs(ty) => ty.as_ref(),
            _ => None,
        }
    }

    pub fn def_namespace(&self) -> &Str {
        &self.vis.def_namespace
    }

    pub fn is_toplevel(&self) -> bool {
        let ns = Str::rc(self.vis.def_namespace.trim_start_matches("./"));
        ns.split_with(&[".", "::"]).len() == 1
    }

    pub fn is_fast_value(&self) -> bool {
        !self.is_toplevel()
            && !self.is_parameter()
            && self.ctx.control_kind().is_none()
            && (self.ctx.is_subr() || self.ctx.is_instant())
    }
}