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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
//! Defines `Context`.
//!
//! `Context` is used for type inference and type checking.
#![allow(clippy::result_unit_err)]
#![allow(clippy::assigning_clones)]
pub mod compare;
pub mod eval;
pub mod generalize;
pub mod hint;
pub mod initialize;
pub mod inquire;
pub mod instantiate;
pub mod instantiate_spec;
pub mod register;
pub mod test;
pub mod unify;

use std::fmt;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::option::Option; // conflicting to Type::Option
use std::path::Path;

use erg_common::config::ErgConfig;
use erg_common::consts::DEBUG_MODE;
use erg_common::consts::ERG_MODE;
use erg_common::consts::PYTHON_MODE;
use erg_common::dict::Dict;
use erg_common::error::Location;
use erg_common::impl_display_from_debug;
use erg_common::pathutil::NormalizedPathBuf;
use erg_common::traits::{Locational, Stream};
use erg_common::Str;
use erg_common::{fmt_option, fn_name, get_hash, log};

use ast::{DefId, DefKind, VarName};
use erg_parser::ast;
use erg_parser::ast::Def;
use erg_parser::ast::Signature;
use erg_parser::token::Token;

use crate::context::instantiate::TyVarCache;
use crate::context::instantiate_spec::ConstTemplate;
use crate::error::{TyCheckError, TyCheckErrors};
use crate::hir::Identifier;
use crate::module::SharedModuleGraph;
use crate::module::{
    SharedCompilerResource, SharedModuleCache, SharedModuleIndex, SharedPromises, SharedTraitImpls,
};
use crate::ty::value::ValueObj;
use crate::ty::GuardType;
use crate::ty::ParamTy;
use crate::ty::{Predicate, Type, Visibility, VisibilityModifier};
use crate::varinfo::{AbsLocation, Mutability, VarInfo, VarKind};
use Type::*;

/// For implementing LSP or other IDE features
pub trait ContextProvider {
    fn dir(&self) -> Dict<&VarName, &VarInfo>;
    fn get_receiver_ctx(&self, receiver_name: &str) -> Option<&Context>;
    fn get_var_info(&self, name: &str) -> Option<(&VarName, &VarInfo)>;
    fn has(&self, name: &str) -> bool {
        self.get_var_info(name).is_some()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ControlKind {
    If,
    While,
    For,
    Match,
    Try,
    With,
    Discard,
    Assert,
}

impl fmt::Display for ControlKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::If => write!(f, "if"),
            Self::While => write!(f, "while"),
            Self::For => write!(f, "for"),
            Self::Match => write!(f, "match"),
            Self::Try => write!(f, "try"),
            Self::With => write!(f, "with"),
            Self::Discard => write!(f, "discard"),
            Self::Assert => write!(f, "assert"),
        }
    }
}

impl TryFrom<&str> for ControlKind {
    type Error = ();
    fn try_from(s: &str) -> Result<Self, Self::Error> {
        match s {
            "if" | "if!" => Ok(ControlKind::If),
            "while!" => Ok(ControlKind::While),
            "while" if PYTHON_MODE => Ok(ControlKind::While),
            "for" | "for!" => Ok(ControlKind::For),
            "match" | "match!" => Ok(ControlKind::Match),
            "try" | "try!" => Ok(ControlKind::Try),
            "with" | "with!" => Ok(ControlKind::With),
            "discard" => Ok(ControlKind::Discard),
            "assert" => Ok(ControlKind::Assert),
            _ => Err(()),
        }
    }
}

impl ControlKind {
    pub const fn is_if(&self) -> bool {
        matches!(self, Self::If)
    }
    /// if | if! | while!
    pub const fn is_conditional(&self) -> bool {
        matches!(self, Self::If | Self::While)
    }
    pub const fn makes_scope(&self) -> bool {
        !matches!(self, Self::Assert)
    }
}

#[derive(Debug, Clone)]
pub struct TraitImpl {
    pub sub_type: Type,
    pub sup_trait: Type,
    pub declared_in: Option<NormalizedPathBuf>,
}

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

impl Eq for TraitImpl {}

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

impl std::fmt::Display for TraitImpl {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "TraitImpl{{{} <: {}}}", self.sub_type, self.sup_trait)
    }
}

impl TraitImpl {
    pub const fn new(
        sub_type: Type,
        sup_trait: Type,
        declared_in: Option<NormalizedPathBuf>,
    ) -> Self {
        Self {
            sub_type,
            sup_trait,
            declared_in,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ClassDefType {
    Simple(Type),
    ImplTrait { class: Type, impl_trait: Type },
}

impl std::fmt::Display for ClassDefType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ClassDefType::Simple(ty) => write!(f, "{ty}"),
            ClassDefType::ImplTrait { class, impl_trait } => {
                write!(f, "{class}|<: {impl_trait}|")
            }
        }
    }
}

impl ClassDefType {
    pub const fn impl_trait(class: Type, impl_trait: Type) -> Self {
        ClassDefType::ImplTrait { class, impl_trait }
    }

    pub fn get_class(&self) -> &Type {
        match self {
            ClassDefType::Simple(class) => class,
            ClassDefType::ImplTrait { class, .. } => class,
        }
    }

    pub fn get_impl_trait(&self) -> Option<&Type> {
        match self {
            ClassDefType::Simple(_) => None,
            ClassDefType::ImplTrait { impl_trait, .. } => Some(impl_trait),
        }
    }

    pub fn is_class_of(&self, t: &Type) -> bool {
        match self {
            ClassDefType::Simple(class) => class == t,
            ClassDefType::ImplTrait { class, .. } => class == t,
        }
    }

    pub fn is_impl_of(&self, trait_: &Type) -> bool {
        match self {
            ClassDefType::ImplTrait { impl_trait, .. } => impl_trait == trait_,
            _ => false,
        }
    }
}

#[derive(Debug, Clone)]
pub struct MethodContext {
    pub id: DefId,
    pub typ: ClassDefType,
    pub ctx: Context,
}

impl Deref for MethodContext {
    type Target = Context;

    fn deref(&self) -> &Self::Target {
        &self.ctx
    }
}

impl DerefMut for MethodContext {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.ctx
    }
}

impl MethodContext {
    pub const fn new(id: DefId, typ: ClassDefType, ctx: Context) -> Self {
        Self { id, typ, ctx }
    }
}

#[derive(Debug, Clone, Default)]
pub struct TypeContext {
    pub typ: Type,
    pub ctx: Context,
}

impl Deref for TypeContext {
    type Target = Context;

    fn deref(&self) -> &Self::Target {
        &self.ctx
    }
}

impl DerefMut for TypeContext {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.ctx
    }
}

impl TypeContext {
    pub const fn new(typ: Type, ctx: Context) -> Self {
        Self { typ, ctx }
    }

    pub(crate) fn get_class_attr<'c>(
        &'c self,
        name: &VarName,
        ctx: &'c Context,
    ) -> Option<&'c VarInfo> {
        self.get_current_scope_attr(name).or_else(|| {
            let sups = ctx.get_nominal_super_type_ctxs(&self.typ)?;
            for sup in sups {
                if let Some(vi) = sup.get_current_scope_attr(name) {
                    return Some(vi);
                }
            }
            None
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DefaultInfo {
    NonDefault, // var-args should be non-default
    WithDefault,
}

impl_display_from_debug!(DefaultInfo);

impl DefaultInfo {
    pub const fn has_default(&self) -> bool {
        matches!(self, DefaultInfo::WithDefault)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Variance {
    /// Output(T), 共変, +
    Covariant,
    /// Input(T), 反変, -
    Contravariant,
    /// 不変, 0
    #[default]
    Invariant,
}

impl_display_from_debug!(Variance);

impl std::ops::Mul for Variance {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        match (self, rhs) {
            (Variance::Covariant, Variance::Covariant)
            | (Variance::Contravariant, Variance::Contravariant) => Variance::Covariant,
            (Variance::Covariant, Variance::Contravariant)
            | (Variance::Contravariant, Variance::Covariant) => Variance::Contravariant,
            _ => Variance::Invariant,
        }
    }
}

impl Variance {
    pub const fn is_invariant(&self) -> bool {
        matches!(self, Variance::Invariant)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ParamSpec {
    pub(crate) name: Option<Str>,
    // TODO: `:` or `<:`
    pub(crate) t: Type,
    pub is_var_params: bool,
    pub default_info: DefaultInfo,
    loc: AbsLocation,
}

impl ParamSpec {
    pub fn new<S: Into<Str>>(
        name: Option<S>,
        t: Type,
        is_var_params: bool,
        default: DefaultInfo,
        loc: AbsLocation,
    ) -> Self {
        Self {
            name: name.map(|s| s.into()),
            t,
            is_var_params,
            default_info: default,
            loc,
        }
    }

    pub fn named<S: Into<Str>>(
        name: S,
        t: Type,
        is_var_params: bool,
        default: DefaultInfo,
    ) -> Self {
        Self::new(
            Some(name),
            t,
            is_var_params,
            default,
            AbsLocation::unknown(),
        )
    }

    pub fn named_nd<S: Into<Str>>(name: S, t: Type) -> Self {
        Self::new(
            Some(name),
            t,
            false,
            DefaultInfo::NonDefault,
            AbsLocation::unknown(),
        )
    }

    pub fn default<S: Into<Str>>(name: S, t: Type) -> Self {
        Self::new(
            Some(name),
            t,
            false,
            DefaultInfo::WithDefault,
            AbsLocation::unknown(),
        )
    }

    pub fn t<S: Into<Str>>(name: S, is_var_params: bool, default: DefaultInfo) -> Self {
        Self::new(
            Some(name),
            Type,
            is_var_params,
            default,
            AbsLocation::unknown(),
        )
    }

    pub fn t_nd<S: Into<Str>>(name: S) -> Self {
        Self::new(
            Some(name),
            Type,
            false,
            DefaultInfo::NonDefault,
            AbsLocation::unknown(),
        )
    }

    pub fn has_default(&self) -> bool {
        self.default_info.has_default()
    }
}

impl From<&ParamSpec> for ParamTy {
    fn from(param: &ParamSpec) -> Self {
        if let Some(name) = &param.name {
            ParamTy::kw(name.clone(), param.t.clone())
        } else {
            ParamTy::Pos(param.t.clone())
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ContextKind {
    LambdaFunc(Option<ControlKind>),
    LambdaProc(Option<ControlKind>),
    Func,
    Proc,
    Class,
    MethodDefs(Option<Type>), // Type: trait implemented
    PatchMethodDefs(Type),
    Trait,
    StructuralTrait,
    Patch(Type),
    StructuralPatch(Type),
    GluePatch(TraitImpl), // TODO: deprecate (integrate into Patch)
    Module,
    Instant,
    Record,
    Dummy,
}

impl From<&Def> for ContextKind {
    fn from(def: &Def) -> Self {
        match def.def_kind() {
            DefKind::Class | DefKind::Inherit => Self::Class,
            DefKind::Trait | DefKind::Subsume => Self::Trait,
            DefKind::StructuralTrait => Self::StructuralTrait,
            DefKind::ErgImport | DefKind::PyImport | DefKind::RsImport | DefKind::InlineModule => {
                Self::Module
            }
            DefKind::Other => {
                if let Signature::Subr(subr) = &def.sig {
                    if subr.ident.is_procedural() {
                        Self::Proc
                    } else {
                        Self::Func
                    }
                } else {
                    Self::Instant
                }
            }
            // FIXME: Patch(Type),
            DefKind::Patch => Self::Instant,
        }
    }
}

impl fmt::Display for ContextKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::LambdaFunc(kind) => write!(f, "LambdaFunc({})", fmt_option!(kind)),
            Self::LambdaProc(kind) => write!(f, "LambdaProc({})", fmt_option!(kind)),
            Self::Func => write!(f, "Func"),
            Self::Proc => write!(f, "Proc"),
            Self::Class => write!(f, "Class"),
            Self::MethodDefs(trait_) => write!(f, "MethodDefs({})", fmt_option!(trait_)),
            Self::PatchMethodDefs(type_) => write!(f, "PatchMethodDefs({type_})"),
            Self::Trait => write!(f, "Trait"),
            Self::StructuralTrait => write!(f, "StructuralTrait"),
            Self::Patch(type_) => write!(f, "Patch({type_})"),
            Self::StructuralPatch(type_) => write!(f, "StructuralPatch({type_})"),
            Self::GluePatch(type_) => write!(f, "GluePatch({type_})"),
            Self::Module => write!(f, "Module"),
            Self::Instant => write!(f, "Instant"),
            Self::Record => write!(f, "Record"),
            Self::Dummy => write!(f, "Dummy"),
        }
    }
}

impl ContextKind {
    pub const fn is_method_def(&self) -> bool {
        matches!(self, Self::MethodDefs(_))
    }

    pub const fn is_trait_impl(&self) -> bool {
        matches!(self, Self::MethodDefs(Some(_)))
    }

    pub const fn is_type(&self) -> bool {
        matches!(self, Self::Class | Self::Trait | Self::StructuralTrait)
    }

    pub const fn is_subr(&self) -> bool {
        matches!(
            self,
            Self::Func | Self::Proc | Self::LambdaFunc(_) | Self::LambdaProc(_)
        )
    }

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

    pub const fn is_trait(&self) -> bool {
        matches!(self, Self::Trait | Self::StructuralTrait)
    }

    pub const fn is_patch(&self) -> bool {
        matches!(self, Self::Patch(_) | Self::GluePatch(_))
    }

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

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

    pub const fn control_kind(&self) -> Option<ControlKind> {
        match self {
            Self::LambdaFunc(kind) | Self::LambdaProc(kind) => *kind,
            _ => None,
        }
    }
}

/// Indicates the mode registered in the Context
/// Preregister: subroutine or constant expression, can be forward referenced
/// Normal: Cannot be forward referenced
/// 環境に登録されているモードを表す
/// Preregister: サブルーチンまたは定数式、前方参照できる
/// Normal: 前方参照できない
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RegistrationMode {
    PreRegister,
    Normal,
}

impl RegistrationMode {
    pub const fn is_preregister(&self) -> bool {
        matches!(self, Self::PreRegister)
    }
    pub const fn is_normal(&self) -> bool {
        matches!(self, Self::Normal)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ContextInfo {
    mod_id: usize,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MethodPair {
    definition_type: Type,
    method_info: VarInfo,
}

impl fmt::Display for MethodPair {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{{ def: {} info: {} }}",
            self.definition_type, self.method_info
        )
    }
}

impl MethodPair {
    pub const fn new(definition_type: Type, method_info: VarInfo) -> Self {
        Self {
            definition_type,
            method_info,
        }
    }
}

/// Represents the context of the current scope
///
/// Recursive functions/methods are highlighted with the prefix `rec_`, as performance may be significantly degraded.
#[derive(Debug, Clone)]
pub struct Context {
    pub name: Str,
    pub kind: ContextKind,
    pub(crate) cfg: ErgConfig,
    pub(crate) preds: Vec<Predicate>,
    /// for looking up the parent scope
    pub(crate) outer: Option<Box<Context>>,
    // e.g. { "Add": [ConstObjTemplate::App("Self", vec![])])
    pub(crate) const_param_defaults: Dict<Str, Vec<ConstTemplate>>,
    // Superclasses/supertraits by a patch are not included here
    // patchによってsuper class/traitになったものはここに含まれない
    pub(crate) super_classes: Vec<Type>, // if self is a patch, means patch classes
    pub(crate) super_traits: Vec<Type>,  // if self is not a trait, means implemented traits
    // method definitions, if the context is a type
    // specializations are included and needs to be separated out
    pub(crate) methods_list: Vec<MethodContext>,
    // K: method name, V: types defines the method
    // If it is declared in a trait, it takes precedence over the class.
    pub(crate) method_to_traits: Dict<Str, Vec<MethodPair>>,
    pub(crate) method_to_classes: Dict<Str, Vec<MethodPair>>,
    /// K: method name, V: impl patch
    /// Provided methods can switch implementations on a scope-by-scope basis
    /// K: メソッド名, V: それを実装するパッチたち
    /// 提供メソッドはスコープごとに実装を切り替えることができる
    pub(crate) method_impl_patches: Dict<VarName, Vec<VarName>>,
    /// stores declared names (not initialized)
    pub(crate) decls: Dict<VarName, VarInfo>,
    /// for error reporting
    pub(crate) future_defined_locals: Dict<VarName, VarInfo>,
    pub(crate) deleted_locals: Dict<VarName, VarInfo>,
    // stores defined names
    // 型の一致はHashMapでは判定できないため、keyはVarNameとして1つずつ見ていく
    /// ```python
    /// f [x, y], z = ...
    /// ```
    /// => params: vec![(None, [T; 2]), (Some("z"), U)]
    /// => locals: {"x": T, "y": T}
    /// TODO: impl params desugaring and replace to `Dict`
    pub(crate) params: Vec<(Option<VarName>, VarInfo)>,
    pub(crate) params_spec: Vec<ParamSpec>,
    pub(crate) locals: Dict<VarName, VarInfo>,
    pub(crate) consts: Dict<VarName, ValueObj>,
    // {"Nat": ctx, "Int": ctx, ...}
    pub(crate) mono_types: Dict<VarName, TypeContext>,
    // Implementation Contexts for Polymorphic Types
    // Vec<TyParam> are specialization parameters
    // e.g. {"List": [(List(Nat), ctx), (List(Int), ctx), (List(Str), ctx), (List(Obj), ctx), (List('T), ctx)], ...}
    pub(crate) poly_types: Dict<VarName, TypeContext>,
    // patches can be accessed like normal records
    // but when used as a fallback to a type, values are traversed instead of accessing by keys
    pub(crate) patches: Dict<VarName, Context>,
    pub(crate) shared: Option<SharedCompilerResource>,
    pub(crate) tv_cache: Option<TyVarCache>,
    pub(crate) higher_order_caller: Vec<Str>,
    pub(crate) guards: Vec<GuardType>,
    pub(crate) erg_to_py_names: Dict<Str, Str>,
    pub(crate) captured_names: Vec<Identifier>,
    pub(crate) level: usize,
}

impl Default for Context {
    #[inline]
    fn default() -> Self {
        Self::default_with_name("<dummy>")
    }
}

impl fmt::Display for Context {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Context")
            .field("name", &self.name)
            .field("preds", &self.preds)
            .field("params", &self.params)
            .field("decls", &self.decls)
            .field("locals", &self.params)
            .field("consts", &self.consts)
            .field("mono_types", &self.mono_types)
            .field("poly_types", &self.poly_types)
            .field("patches", &self.patches)
            // .field("mod_cache", &self.mod_cache)
            .finish()
    }
}

impl ContextProvider for Context {
    fn dir(&self) -> Dict<&VarName, &VarInfo> {
        let mut vars = self.type_dir(self);
        if let Some(outer) = self.get_outer_scope() {
            vars.guaranteed_extend(outer.dir());
        } else if let Some(builtins) = self.get_builtins() {
            vars.guaranteed_extend(builtins.locals.iter());
        }
        vars
    }

    fn get_receiver_ctx(&self, receiver_name: &str) -> Option<&Context> {
        self.get_mod(receiver_name)
            .or_else(|| self.rec_local_get_type(receiver_name).map(|ctx| &ctx.ctx))
            .or_else(|| {
                let (_, vi) = self.get_var_info(receiver_name)?;
                self.get_nominal_type_ctx(&vi.t).map(|ctx| &ctx.ctx)
            })
    }

    // this is internally recursive
    fn get_var_info(&self, name: &str) -> Option<(&VarName, &VarInfo)> {
        self.get_var_kv(name).or_else(|| {
            self.get_builtins()
                .and_then(|builtin| builtin.get_var_kv(name))
        })
    }
}

impl Context {
    pub fn dir(&self) -> Dict<&VarName, &VarInfo> {
        ContextProvider::dir(self)
    }

    pub fn get_receiver_ctx(&self, receiver_name: &str) -> Option<&Context> {
        ContextProvider::get_receiver_ctx(self, receiver_name)
    }

    pub fn get_receiver_ctxs(&self, receiver_name: &str) -> Vec<&Context> {
        let mut ctxs = vec![];
        if let Some(receiver_ctx) = self.get_receiver_ctx(receiver_name) {
            ctxs.push(receiver_ctx);
            ctxs.extend(
                receiver_ctx
                    .super_classes
                    .iter()
                    .flat_map(|t| self.get_nominal_type_ctx(t).map(|ctx| &ctx.ctx)),
            );
        }
        ctxs
    }

    pub fn get_var_info(&self, name: &str) -> Option<(&VarName, &VarInfo)> {
        ContextProvider::get_var_info(self, name)
    }

    pub fn has(&self, name: &str) -> bool {
        ContextProvider::has(self, name)
    }

    pub fn get_type_info(&self, typ: &Type) -> Option<(&VarName, &VarInfo)> {
        let namespace = typ.namespace();
        let ctx = self.get_namespace(&namespace)?;
        ctx.get_var_info(&typ.local_name())
    }

    pub fn unregister(&mut self, name: &str) -> Option<VarInfo> {
        self.mono_types.remove(name);
        self.poly_types.remove(name);
        self.patches.remove(name);
        self.erg_to_py_names.remove(name);
        self.locals
            .remove(name)
            .or_else(|| self.locals.remove(name))
    }
}

impl Context {
    #[allow(clippy::too_many_arguments)]
    #[inline]
    pub fn new(
        name: Str,
        cfg: ErgConfig,
        kind: ContextKind,
        params: Vec<ParamSpec>,
        outer: Option<Context>,
        shared: Option<SharedCompilerResource>,
        level: usize,
    ) -> Self {
        Self::with_capacity(name, cfg, kind, params, outer, shared, 0, level)
    }

    pub fn default_with_name(name: &'static str) -> Self {
        Self::new(
            name.into(),
            ErgConfig::default(),
            ContextKind::Dummy,
            vec![],
            None,
            None,
            Self::TOP_LEVEL,
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub fn with_capacity(
        name: Str,
        cfg: ErgConfig,
        kind: ContextKind,
        params: Vec<ParamSpec>,
        outer: Option<Context>,
        shared: Option<SharedCompilerResource>,
        capacity: usize,
        level: usize,
    ) -> Self {
        let mut params_ = Vec::new();
        for param in params.clone().into_iter() {
            let id = DefId(get_hash(&(&name, &param)));
            if let Some(name) = param.name {
                let var_kind = VarKind::parameter(id, param.is_var_params, param.default_info);
                let muty = Mutability::from(&name[..]);
                let vi = VarInfo::new(
                    param.t,
                    muty,
                    Visibility::private(&name),
                    var_kind,
                    None,
                    kind.clone(),
                    None,
                    param.loc,
                );
                params_.push((Some(VarName::new(Token::symbol(&name))), vi));
            } else {
                let var_kind = VarKind::parameter(id, param.is_var_params, param.default_info);
                let muty = Mutability::Immutable;
                let vi = VarInfo::new(
                    param.t,
                    muty,
                    Visibility::private(name.clone()),
                    var_kind,
                    None,
                    kind.clone(),
                    None,
                    param.loc,
                );
                params_.push((None, vi));
            }
        }
        Self {
            name,
            cfg,
            kind,
            preds: vec![],
            outer: outer.map(Box::new),
            super_classes: vec![],
            super_traits: vec![],
            methods_list: vec![],
            const_param_defaults: Dict::default(),
            method_to_traits: Dict::default(),
            method_to_classes: Dict::default(),
            method_impl_patches: Dict::default(),
            params: params_,
            params_spec: params,
            decls: Dict::default(),
            future_defined_locals: Dict::default(),
            deleted_locals: Dict::default(),
            locals: Dict::with_capacity(capacity),
            consts: Dict::default(),
            mono_types: Dict::default(),
            poly_types: Dict::default(),
            shared,
            tv_cache: None,
            patches: Dict::default(),
            higher_order_caller: vec![],
            guards: vec![],
            erg_to_py_names: Dict::default(),
            captured_names: vec![],
            level,
        }
    }

    #[inline]
    pub fn mono(
        name: Str,
        cfg: ErgConfig,
        kind: ContextKind,
        outer: Option<Context>,
        shared: Option<SharedCompilerResource>,
        level: usize,
    ) -> Self {
        Self::new(name, cfg, kind, vec![], outer, shared, level)
    }

    #[allow(clippy::too_many_arguments)]
    #[inline]
    pub fn poly(
        name: Str,
        cfg: ErgConfig,
        kind: ContextKind,
        params: Vec<ParamSpec>,
        outer: Option<Context>,
        shared: Option<SharedCompilerResource>,
        capacity: usize,
        level: usize,
    ) -> Self {
        Self::with_capacity(name, cfg, kind, params, outer, shared, capacity, level)
    }

    pub fn poly_trait<S: Into<Str>>(
        name: S,
        params: Vec<ParamSpec>,
        cfg: ErgConfig,
        shared: Option<SharedCompilerResource>,
        capacity: usize,
        level: usize,
    ) -> Self {
        let name = name.into();
        Self::poly(
            name,
            cfg,
            ContextKind::Trait,
            params,
            None,
            shared,
            capacity,
            level,
        )
    }

    #[inline]
    pub fn builtin_poly_trait<S: Into<Str>>(
        name: S,
        params: Vec<ParamSpec>,
        capacity: usize,
    ) -> Self {
        Self::poly_trait(
            name,
            params,
            ErgConfig::default(),
            None,
            capacity,
            Self::TOP_LEVEL,
        )
    }

    pub fn poly_class<S: Into<Str>>(
        name: S,
        params: Vec<ParamSpec>,
        cfg: ErgConfig,
        shared: Option<SharedCompilerResource>,
        capacity: usize,
        level: usize,
    ) -> Self {
        let name = name.into();
        Self::poly(
            name,
            cfg,
            ContextKind::Class,
            params,
            None,
            shared,
            capacity,
            level,
        )
    }

    #[inline]
    pub fn builtin_poly_class<S: Into<Str>>(
        name: S,
        params: Vec<ParamSpec>,
        capacity: usize,
    ) -> Self {
        Self::poly_class(
            name,
            params,
            ErgConfig::default(),
            None,
            capacity,
            Self::TOP_LEVEL,
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub fn poly_patch<S: Into<Str>>(
        name: S,
        base: Type,
        params: Vec<ParamSpec>,
        cfg: ErgConfig,
        shared: Option<SharedCompilerResource>,
        capacity: usize,
        level: usize,
    ) -> Self {
        let name = name.into();
        Self::poly(
            name,
            cfg,
            ContextKind::Patch(base),
            params,
            None,
            shared,
            capacity,
            level,
        )
    }

    #[inline]
    pub fn mono_trait<S: Into<Str>>(
        name: S,
        cfg: ErgConfig,
        shared: Option<SharedCompilerResource>,
        capacity: usize,
        level: usize,
    ) -> Self {
        Self::poly_trait(name, vec![], cfg, shared, capacity, level)
    }

    #[inline]
    pub fn builtin_mono_trait<S: Into<Str>>(name: S, capacity: usize) -> Self {
        Self::mono_trait(name, ErgConfig::default(), None, capacity, Self::TOP_LEVEL)
    }

    #[inline]
    pub fn mono_class<S: Into<Str>>(
        name: S,
        cfg: ErgConfig,
        shared: Option<SharedCompilerResource>,
        capacity: usize,
        level: usize,
    ) -> Self {
        Self::poly_class(name, vec![], cfg, shared, capacity, level)
    }

    #[inline]
    pub fn builtin_mono_class<S: Into<Str>>(name: S, capacity: usize) -> Self {
        Self::mono_class(name, ErgConfig::default(), None, capacity, Self::TOP_LEVEL)
    }

    #[inline]
    pub fn mono_patch<S: Into<Str>>(
        name: S,
        base: Type,
        cfg: ErgConfig,
        shared: Option<SharedCompilerResource>,
        capacity: usize,
        level: usize,
    ) -> Self {
        Self::poly_patch(name, base, vec![], cfg, shared, capacity, level)
    }

    #[inline]
    pub fn methods(
        impl_trait: Option<Type>,
        cfg: ErgConfig,
        shared: Option<SharedCompilerResource>,
        capacity: usize,
        level: usize,
    ) -> Self {
        let name = if let Some(tr) = &impl_trait {
            tr.local_name()
        } else {
            Str::ever("Methods")
        };
        Self::with_capacity(
            name,
            cfg,
            ContextKind::MethodDefs(impl_trait),
            vec![],
            None,
            shared,
            capacity,
            level,
        )
    }

    #[inline]
    pub fn builtin_methods(impl_trait: Option<Type>, capacity: usize) -> Self {
        Self::methods(
            impl_trait,
            ErgConfig::default(),
            None,
            capacity,
            Self::TOP_LEVEL,
        )
    }

    #[allow(clippy::too_many_arguments)]
    #[inline]
    pub fn poly_glue_patch<S: Into<Str>>(
        name: S,
        base: Type,
        impls: Type,
        params: Vec<ParamSpec>,
        cfg: ErgConfig,
        shared: Option<SharedCompilerResource>,
        capacity: usize,
        level: usize,
    ) -> Self {
        Self::poly(
            name.into(),
            cfg,
            ContextKind::GluePatch(TraitImpl::new(base, impls, None)),
            params,
            None,
            shared,
            capacity,
            level,
        )
    }

    #[inline]
    pub fn builtin_poly_glue_patch<S: Into<Str>>(
        name: S,
        base: Type,
        impls: Type,
        params: Vec<ParamSpec>,
        capacity: usize,
    ) -> Self {
        Self::poly_glue_patch(
            name,
            base,
            impls,
            params,
            ErgConfig::default(),
            None,
            capacity,
            Self::TOP_LEVEL,
        )
    }

    #[inline]
    pub fn builtin_patch<S: Into<Str>>(
        name: S,
        impls: Type,
        params: Vec<ParamSpec>,
        capacity: usize,
    ) -> Self {
        Self::poly(
            name.into(),
            ErgConfig::default(),
            ContextKind::Patch(impls),
            params,
            None,
            None,
            capacity,
            Self::TOP_LEVEL,
        )
    }

    #[inline]
    pub fn module(
        name: Str,
        cfg: ErgConfig,
        shared: Option<SharedCompilerResource>,
        capacity: usize,
    ) -> Self {
        Self::with_capacity(
            name,
            cfg,
            ContextKind::Module,
            vec![],
            None,
            shared,
            capacity,
            Self::TOP_LEVEL,
        )
    }

    #[inline]
    pub fn builtin_module<S: Into<Str>>(
        name: S,
        cfg: ErgConfig,
        shared: SharedCompilerResource,
        capacity: usize,
    ) -> Self {
        Self::module(name.into(), cfg, Some(shared), capacity)
    }

    #[inline]
    pub fn instant(
        name: Str,
        cfg: ErgConfig,
        capacity: usize,
        shared: Option<SharedCompilerResource>,
        outer: Context,
    ) -> Self {
        Self::with_capacity(
            name,
            cfg,
            ContextKind::Instant,
            vec![],
            Some(outer),
            shared,
            capacity,
            Self::TOP_LEVEL,
        )
    }

    pub(crate) fn module_path(&self) -> &Path {
        self.cfg.input.path()
    }

    pub(crate) fn absolutize(&self, loc: Location) -> AbsLocation {
        AbsLocation::new(Some(NormalizedPathBuf::from(self.module_path())), loc)
    }

    #[inline]
    pub fn caused_by(&self) -> String {
        String::from(&self.name[..])
    }

    /// use `get_outer_scope` for getting the outer scope
    pub(crate) fn get_outer(&self) -> Option<&Context> {
        self.outer.as_ref().map(|x| x.as_ref())
    }

    /// If both `self` and `outer` are modules, returns `None` because the outer namespace is different from the current context
    pub(crate) fn get_outer_scope(&self) -> Option<&Context> {
        let outer = self.get_outer()?;
        if outer.name != "<builtins>" && outer.kind.is_module() && self.kind.is_module() {
            None
        } else {
            Some(outer)
        }
    }

    pub(crate) fn get_outer_scope_or_builtins(&self) -> Option<&Context> {
        self.get_outer_scope().or_else(|| self.get_builtins())
    }

    pub(crate) fn get_mut_outer(&mut self) -> Option<&mut Context> {
        self.outer.as_mut().map(|x| x.as_mut())
    }

    pub(crate) fn impl_of(&self) -> Option<Type> {
        if let ContextKind::MethodDefs(Some(tr)) = &self.kind {
            Some(tr.clone())
        } else {
            None
        }
    }

    pub fn is_empty(&self) -> bool {
        let default_locals = if ERG_MODE { 4 } else { 3 };
        self.super_classes.is_empty()
            && self.super_traits.is_empty()
            && self.methods_list.is_empty()
            && self.const_param_defaults.is_empty()
            && self.method_to_traits.is_empty()
            && self.method_to_classes.is_empty()
            && self.method_impl_patches.is_empty()
            && self.params.is_empty()
            && self.params_spec.is_empty()
            && self.decls.is_empty()
            && self.future_defined_locals.is_empty()
            && self.deleted_locals.is_empty()
            && self.consts.is_empty()
            && self.mono_types.is_empty()
            && self.poly_types.is_empty()
            && self.patches.is_empty()
            && self.locals.len() <= default_locals
    }

    pub(crate) fn path(&self) -> Str {
        // NOTE: maybe this need to be changed if we want to support nested classes/traits
        if self.kind == ContextKind::Module {
            self.name.replace(".__init__", "").into()
        } else if let Some(outer) = self.get_outer() {
            outer.path()
        } else {
            self.name.replace(".__init__", "").into()
        }
    }

    /// Returns None if self is `<builtins>`.
    /// This avoids infinite loops.
    pub(crate) fn get_builtins(&self) -> Option<&Context> {
        // builtins中で定義した型等はmod_cacheがNoneになっている
        if &self.path()[..] != "<builtins>" {
            self.shared
                .as_ref()
                .map(|shared| shared.mod_cache.raw_ref_builtins_ctx().unwrap())
                .map(|mod_ctx| &mod_ctx.context)
        } else {
            None
        }
    }

    pub(crate) fn get_module(&self) -> Option<&Context> {
        self.get_outer()
            .and_then(|outer| {
                if outer.kind == ContextKind::Module {
                    Some(outer)
                } else {
                    outer.get_module()
                }
            })
            .or_else(|| {
                if self.kind == ContextKind::Module {
                    Some(self)
                } else {
                    None
                }
            })
    }

    pub(crate) fn get_module_from_stack(&self, path: &NormalizedPathBuf) -> Option<&Context> {
        if self.kind == ContextKind::Module && &NormalizedPathBuf::from(self.module_path()) == path
        {
            return Some(self);
        }
        self.get_outer()
            .and_then(|outer| outer.get_module_from_stack(path))
    }

    /// This method is intended to be called __only__ in the top-level module.
    /// `.cfg` is not initialized and is used around.
    pub fn initialize(&mut self) {
        let mut shared = mem::take(&mut self.shared);
        if let Some(mod_cache) = shared.as_mut().map(|s| &mut s.mod_cache) {
            mod_cache.initialize();
        }
        if let Some(py_mod_cache) = shared.as_mut().map(|s| &mut s.py_mod_cache) {
            py_mod_cache.initialize();
        }
        *self = Self::new(
            self.name.clone(),
            self.cfg.clone(),
            self.kind.clone(),
            vec![],
            None,
            shared,
            self.level,
        );
    }

    pub(crate) fn grow(
        &mut self,
        name: &str,
        kind: ContextKind,
        vis: VisibilityModifier,
        tv_cache: Option<TyVarCache>,
    ) {
        let name = if kind.is_module() {
            name.into()
        } else if vis.is_public() {
            format!("{parent}.{name}", parent = self.name)
        } else {
            format!("{parent}::{name}", parent = self.name)
        };
        log!(info "{}: current namespace: {name}", fn_name!());
        self.outer = Some(Box::new(mem::take(self)));
        // self.level += 1;
        self.cfg = self.get_outer().unwrap().cfg.clone();
        self.shared = self.get_outer().unwrap().shared.clone();
        self.higher_order_caller = self.get_outer().unwrap().higher_order_caller.clone();
        self.tv_cache = tv_cache;
        self.name = name.into();
        self.kind = kind;
    }

    pub(crate) fn replace(&mut self, new: Self) {
        let old = mem::take(self);
        *self = new;
        self.outer = Some(Box::new(old));
        self.cfg = self.get_outer().unwrap().cfg.clone();
        self.shared = self.get_outer().unwrap().shared.clone();
        self.higher_order_caller = self.get_outer().unwrap().higher_order_caller.clone();
    }

    pub(crate) fn clear_invalid_vars(&mut self) {
        self.locals.retain(|_, v| v.t != Failure);
        self.decls.retain(|_, v| v.t != Failure);
        self.params.retain(|(_, v)| v.t != Failure);
    }

    /// Note that the popped context is detached and `outer == None`.
    pub fn pop(&mut self) -> Context {
        self.check_types();
        if let Some(parent) = self.outer.take() {
            let ctx = mem::take(self);
            *self = *parent;
            log!(info "{}: current namespace: {}", fn_name!(), self.name);
            ctx
        } else {
            panic!("cannot pop the top-level context (or use `pop_mod`)");
        }
    }

    /// unlike `pop`, `outer` must be `None`.
    pub fn pop_mod(&mut self) -> Option<Context> {
        self.check_types();
        if self.outer.is_some() {
            log!(err "not in the top-level context");
            if self.kind.is_module() {
                Some(self.pop())
            } else {
                None
            }
        } else {
            log!(info "{}: current namespace: <builtins>", fn_name!());
            // toplevel
            Some(mem::take(self))
        }
    }

    pub(crate) fn check_decls_and_pop(&mut self) -> (Context, TyCheckErrors) {
        match self.check_decls() {
            Ok(_) => (self.pop(), TyCheckErrors::empty()),
            Err(errs) => (self.pop(), errs),
        }
    }

    pub(crate) fn check_decls(&mut self) -> Result<(), TyCheckErrors> {
        let mut uninited_errs = TyCheckErrors::empty();
        for (name, vi) in self.decls.iter() {
            uninited_errs.push(TyCheckError::uninitialized_error(
                self.cfg.input.clone(),
                line!() as usize,
                name.loc(),
                self.caused_by(),
                name.inspect(),
                &vi.t,
            ));
        }
        if !uninited_errs.is_empty() {
            Err(uninited_errs)
        } else {
            Ok(())
        }
    }

    /// enumerates all the variables/methods in the current context & super contexts.
    fn type_dir<'t>(&'t self, namespace: &'t Context) -> Dict<&VarName, &VarInfo> {
        let mut attrs = self.locals.iter().collect::<Dict<_, _>>();
        attrs.guaranteed_extend(
            self.params
                .iter()
                .filter_map(|(k, v)| k.as_ref().map(|k| (k, v))),
        );
        attrs.guaranteed_extend(self.decls.iter());
        attrs.guaranteed_extend(
            self.methods_list
                .iter()
                .flat_map(|ctx| ctx.type_dir(namespace)),
        );
        if let Some(tv_cache) = &self.tv_cache {
            attrs.guaranteed_extend(tv_cache.var_infos.iter());
        }
        for sup in self.super_classes.iter() {
            if let Some(sup_ctx) = namespace.get_nominal_type_ctx(sup) {
                if sup_ctx.name == self.name {
                    continue;
                }
                attrs.guaranteed_extend(sup_ctx.type_dir(namespace));
            }
        }
        attrs
    }

    pub fn local_dir(&self) -> Dict<&VarName, &VarInfo> {
        self.type_dir(self)
    }

    pub(crate) fn opt_mod_cache(&self) -> Option<&SharedModuleCache> {
        self.shared.as_ref().map(|s| &s.mod_cache)
    }

    pub(crate) fn mod_cache(&self) -> &SharedModuleCache {
        &self.shared().mod_cache
    }

    pub(crate) fn opt_py_mod_cache(&self) -> Option<&SharedModuleCache> {
        self.shared.as_ref().map(|s| &s.py_mod_cache)
    }

    pub(crate) fn py_mod_cache(&self) -> &SharedModuleCache {
        &self.shared().py_mod_cache
    }

    pub(crate) fn opt_index(&self) -> Option<&SharedModuleIndex> {
        self.shared.as_ref().map(|s| &s.index)
    }

    pub fn index(&self) -> &SharedModuleIndex {
        &self.shared().index
    }

    pub fn graph(&self) -> &SharedModuleGraph {
        &self.shared().graph
    }

    pub fn trait_impls(&self) -> &SharedTraitImpls {
        &self.shared().trait_impls
    }

    pub fn shared(&self) -> &SharedCompilerResource {
        self.shared.as_ref().unwrap()
    }

    pub fn promises(&self) -> &SharedPromises {
        &self.shared().promises
    }

    pub fn current_control_flow(&self) -> Option<ControlKind> {
        self.higher_order_caller
            .last()
            .and_then(|name| ControlKind::try_from(&name[..]).ok())
    }

    pub fn control_kind(&self) -> Option<ControlKind> {
        for caller in self.higher_order_caller.iter().rev() {
            if let Ok(control) = ControlKind::try_from(&caller[..]) {
                return Some(control);
            }
        }
        None
    }

    /// Context of the function that actually creates the scope.
    /// Control flow function blocks do not create actual scopes.
    pub fn current_true_function_ctx(&self) -> Option<&Context> {
        if self.kind.is_subr() && self.current_control_flow().is_none() {
            Some(self)
        } else if let Some(outer) = self.get_outer_scope() {
            outer.current_true_function_ctx()
        } else {
            None
        }
    }

    pub(crate) fn check_types(&self) {
        if DEBUG_MODE {
            for (_, ctx) in self.poly_types.iter() {
                if ctx.typ.has_undoable_linked_var() {
                    panic!("{} has undoable linked vars", ctx.typ);
                }
                ctx.check_types();
            }
            for methods in self.methods_list.iter() {
                if methods.typ.get_class().has_undoable_linked_var() {
                    panic!("{} has undoable linked vars", methods.typ);
                }
                if methods
                    .typ
                    .get_impl_trait()
                    .is_some_and(|t| t.has_undoable_linked_var())
                {
                    panic!("{} has undoable linked vars", methods.typ);
                }
                methods.check_types();
            }
            if let Some(outer) = self.get_outer() {
                outer.check_types();
            }
        }
    }

    pub(crate) fn get_tv_ctx(&self, ident: &ast::Identifier, args: &ast::Args) -> TyVarCache {
        let mut tv_ctx = TyVarCache::new(self.level, self);
        if let Some(ctx) = self.get_type_ctx(ident.inspect()) {
            let arg_ts = ctx.params.iter().map(|(_, vi)| &vi.t);
            for ((tp, arg), arg_t) in ctx.typ.typarams().iter().zip(args.pos_args()).zip(arg_ts) {
                let tp = self.detach_tp(tp.clone(), &mut tv_ctx);
                if let ast::Expr::Accessor(ast::Accessor::Ident(ident)) = &arg.expr {
                    if self.subtype_of(arg_t, &Type::Type) {
                        if let Ok(tv) = self.convert_tp_into_type(tp.clone()) {
                            let _ = tv_ctx.push_or_init_tyvar(&ident.name, &tv, self);
                            continue;
                        }
                    }
                    let _ = tv_ctx.push_or_init_typaram(&ident.name, &tp, self);
                }
            }
        }
        tv_ctx
    }
}

#[derive(Debug, Clone, Default)]
pub struct ModuleContext {
    pub context: Context,
    pub scope: Dict<Str, Context>,
}

impl ModuleContext {
    pub const fn new(toplevel: Context, scope: Dict<Str, Context>) -> Self {
        Self {
            context: toplevel,
            scope,
        }
    }

    pub fn get_top_cfg(&self) -> ErgConfig {
        self.context.cfg.clone()
    }

    pub fn is_empty(&self) -> bool {
        self.context.is_empty()
    }
}