veryl-analyzer 0.21.0

A modern hardware description language
Documentation
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
use crate::analyzer_error::{AnalyzerError, ExceedLimitKind};
use crate::conv::conv_profiler::{ConvProfile, ConvProfileGuard};
use crate::conv::instance::{InstanceHistory, InstanceHistoryError};
use crate::ir::{
    Component, Comptime, Declaration, Expression, FfClock, FfReset, FuncPath, Function, Interface,
    IrResult, ShapeRef, Signature, Statement, Type, VarId, VarIndex, VarKind, VarPath, VarSelect,
    Variable, VariableInfo,
};
use crate::namespace::Namespace;
use crate::scope;
use crate::symbol::{Affiliation, ClockDomain, Direction, GenericMap, SymbolId};
use crate::symbol_path::GenericSymbolPath;
use crate::value::MaskCache;
use crate::{HashMap, HashSet};
use miette::Result;
use std::sync::{Arc, Mutex};
use veryl_parser::resource_table::StrId;
use veryl_parser::token_range::TokenRange;
use veryl_parser::veryl_grammar_trait::{ComponentInstantiation, Identifier};
use veryl_parser::veryl_token::Token;

#[derive(Clone)]
pub struct Config {
    pub retain_component_body: bool,
    pub instance_depth_limit: usize,
    pub instance_total_limit: usize,
    pub function_instance_depth_limit: usize,
    pub evaluate_size_limit: usize,
    pub evaluate_array_limit: usize,
    pub defines: HashSet<StrId>,
}

#[derive(Clone, Debug, Default)]
pub(crate) struct RuntimeFunctionEffect {
    pub external_write: bool,
    pub written_outputs: HashSet<VarPath>,
}

impl RuntimeFunctionEffect {
    pub fn written_paths_for<'a>(
        &'a self,
        formal: &'a VarPath,
    ) -> impl Iterator<Item = &'a VarPath> {
        self.written_outputs
            .iter()
            .filter(move |path| path.0.starts_with(&formal.0))
    }
}

#[derive(Clone, Debug)]
pub(crate) struct FunctionOutputBinding {
    pub scheduler_external: bool,
}

#[derive(Clone, Debug)]
struct FunctionEffectFrame {
    path: FuncPath,
    outputs: HashMap<VarId, FunctionOutputBinding>,
    effect: RuntimeFunctionEffect,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            retain_component_body: false,
            instance_depth_limit: 1024,
            instance_total_limit: 1024 * 1024,
            function_instance_depth_limit: 24,
            evaluate_size_limit: 1024 * 1024,
            evaluate_array_limit: 128,
            defines: HashSet::default(),
        }
    }
}

#[derive(Default)]
pub struct Context {
    pub config: Config,
    pub var_id: VarId,
    pub var_paths: HashMap<VarPath, (VarId, Comptime)>,
    pub func_paths: HashMap<FuncPath, VarId>,
    /// Symbols whose function bodies are mid-conversion. Carried (via `inherit`)
    /// across the fresh per-call contexts that mutual recursion creates, so
    /// re-entry is detected and the IR conversion doesn't overflow.
    pub converting_funcs: Vec<SymbolId>,
    pub variables: HashMap<VarId, Variable>,
    pub functions: HashMap<VarId, Function>,
    pub port_types: HashMap<VarPath, (Type, ClockDomain)>,
    pub modports: HashMap<StrId, Vec<(StrId, Direction)>>,
    pub declarations: Vec<Declaration>,
    pub default_clock: Option<(VarPath, SymbolId)>,
    pub default_reset: Option<(VarPath, SymbolId)>,
    pub inst_signatures: HashMap<StrId, Signature>,
    pub modport_signatures: Vec<HashMap<StrId, Signature>>,
    pub instance_history: InstanceHistory,
    /// Recursion depth of `eval_factor_path`, bounded by `function_instance_depth_limit`.
    pub function_eval_depth: usize,
    /// Recorded when `function_eval_depth`'s limit is hit, since the eval path
    /// may swallow an inserted error.
    pub function_eval_overflow: Option<(TokenRange, usize)>,
    /// Recorded when a comptime for loop exceeds evaluate_size_limit; skipping
    /// its body would silently fold consts to their pre-loop values.
    pub comptime_for_overflow: Option<TokenRange>,
    /// Active generic-function call signatures, to detect exact re-entry as
    /// infinite recursion. Not `instance_history`: that only marks a signature
    /// complete via `set_instance_history`, which functions never call, so it
    /// would stay wedged as "active" forever.
    pub function_call_stack: Vec<Signature>,
    function_effects: HashMap<FuncPath, RuntimeFunctionEffect>,
    function_effect_stack: Vec<FunctionEffectFrame>,
    /// Return variable of the function currently being constant-evaluated, and
    /// whether one of its `return` statements has already run. A `return` is
    /// lowered to an assignment to that variable, so without this the
    /// evaluator walks straight past it and the LAST `return` wins.
    pub function_ret_var: Option<VarId>,
    pub function_returned: bool,
    pub select_paths: Vec<(VarPath, GenericSymbolPath)>,
    pub select_dims: Vec<usize>,
    pub ignore_var_func: bool,
    pub disalbe_const_opt: bool,
    pub namespaces: Vec<Namespace>,
    pub in_generic: bool,
    pub allow_component_as_factor: bool,
    pub in_test_module: bool,
    pub in_dependency: bool,
    pub in_global_func: Option<Token>,
    pub in_if_reset: bool,
    /// Inside an initial/final block (testbench statement context).
    pub in_tb_block: bool,
    /// Inside an `initial` block, holding the next `VarId` at its entry so the
    /// block's own procedural locals can be told from the module's state.
    /// `in_tb_block` is unusable here: it also covers `final` and connections.
    pub in_initial: Option<VarId>,
    /// Sink for component method calls hoisted out of the current
    /// testbench statement's expressions; `Some` only while initial/final
    /// statements convert. Each hoisted call runs as its own zero-time
    /// statement immediately before the enclosing one.
    pub tb_hoist: Option<Vec<crate::ir::Statement>>,
    /// Names hoist temporaries uniquely within a module.
    pub tb_hoist_count: usize,
    /// Resolved numeric parameters of the external component instances
    /// declared so far, for evaluating declared method width expressions.
    pub tb_component_params:
        crate::HashMap<veryl_parser::resource_table::StrId, Vec<(String, u64)>>,
    /// Elaborated components of module instances declared so far in the
    /// current module, for hierarchical references from testbench code.
    /// Child module bodies convert in fresh contexts (`inherit` does not
    /// carry this map), so it only holds the current module's instances.
    pub inst_components: HashMap<StrId, Arc<Component>>,
    /// Inside an inst input-port-connection conversion; consumed by
    /// `check_compatibility` to pick the clock/reset diagnostic.
    pub in_inst_port: bool,
    pub current_clock: Option<Comptime>,
    /// Comptimes of the enclosing if/case/switch statement conditions: a
    /// condition gates the writes below it like a mux select, so its clock
    /// domain is checked against each assignment destination.
    pub condition_domains: Vec<Comptime>,
    pub mask_cache: MaskCache,
    pub tb_reset_cycles: HashMap<StrId, Expression>,
    pub tb_clock_period: HashMap<StrId, Expression>,
    pub tb_reset_clock: HashMap<StrId, StrId>,
    /// Sparse rollback state used by analyses that reuse this otherwise-large
    /// context across independent procedures. Empty during normal conversion.
    analysis_transactions: Vec<AnalysisTransaction>,
    hierarchy: Vec<StrId>,
    hierarchical_variables: Vec<Vec<VarPath>>,
    hierarchical_functions: Vec<Vec<FuncPath>>,
    shadowed_variables: HashMap<VarPath, Vec<(VarId, Comptime)>>,
    affiliation: Vec<Affiliation>,
    overrides: Vec<HashMap<VarPath, (Comptime, Expression)>>,
    generic_maps: Vec<Vec<GenericMap>>,
    /// Typedefs currently being expanded into an IR type, to break cyclic
    /// `type A = B; type B = A` chains that `eval_type` would otherwise follow
    /// forever (type_dag reports the cycle, but `dump` evaluates anyway).
    typedef_visiting: Vec<SymbolId>,
    errors: Vec<AnalyzerError>,
    profiler: Option<Arc<Mutex<ConvProfile>>>,
    project_name: Option<String>,
}

struct AnalysisTransaction {
    variables: HashMap<VarId, Variable>,
    comptime_for_overflow: Option<TokenRange>,
    disable_const_opt: bool,
    function_eval_depth: usize,
    function_eval_overflow: Option<(TokenRange, usize)>,
    function_call_stack: Vec<Signature>,
    errors: Vec<AnalyzerError>,
}

impl Context {
    pub(crate) fn begin_analysis_transaction(&mut self) {
        self.analysis_transactions.push(AnalysisTransaction {
            variables: HashMap::default(),
            comptime_for_overflow: self.comptime_for_overflow,
            disable_const_opt: self.disalbe_const_opt,
            function_eval_depth: self.function_eval_depth,
            function_eval_overflow: self.function_eval_overflow.take(),
            function_call_stack: std::mem::take(&mut self.function_call_stack),
            errors: std::mem::take(&mut self.errors),
        });
        self.function_eval_depth = 0;
        self.comptime_for_overflow = None;
    }

    pub(crate) fn rollback_analysis_transaction(&mut self) {
        let transaction = self
            .analysis_transactions
            .pop()
            .expect("analysis transaction is active");
        for (id, variable) in transaction.variables {
            self.variables.insert(id, variable);
        }
        self.comptime_for_overflow = transaction.comptime_for_overflow;
        self.disalbe_const_opt = transaction.disable_const_opt;
        self.function_eval_depth = transaction.function_eval_depth;
        self.function_eval_overflow = transaction.function_eval_overflow;
        self.function_call_stack = transaction.function_call_stack;
        self.errors = transaction.errors;
    }

    /// Get a variable for mutation while remembering its prior value in the
    /// innermost reusable-analysis transaction. Outside such an analysis this
    /// is identical to `variables.get_mut`.
    pub(crate) fn variable_mut(&mut self, id: &VarId) -> Option<&mut Variable> {
        if self
            .analysis_transactions
            .last()
            .is_some_and(|transaction| !transaction.variables.contains_key(id))
        {
            let previous = self.variables.get(id)?.clone();
            self.analysis_transactions
                .last_mut()
                .expect("checked above")
                .variables
                .insert(*id, previous);
        }
        self.variables.get_mut(id)
    }

    /// Run `f` with `cond` on the statement-condition stack (popped before
    /// `?` can skip it).
    pub fn with_condition_domain<T>(
        &mut self,
        cond: Comptime,
        f: impl FnOnce(&mut Self) -> T,
    ) -> T {
        self.condition_domains.push(cond);
        let ret = f(self);
        self.condition_domains.pop();
        ret
    }

    pub fn inherit(&mut self, tgt: &mut Context) {
        std::mem::swap(&mut self.overrides, &mut tgt.overrides);
        std::mem::swap(&mut self.generic_maps, &mut tgt.generic_maps);
        std::mem::swap(&mut self.modport_signatures, &mut tgt.modport_signatures);
        std::mem::swap(&mut self.instance_history, &mut tgt.instance_history);
        std::mem::swap(&mut self.function_eval_depth, &mut tgt.function_eval_depth);
        std::mem::swap(
            &mut self.function_eval_overflow,
            &mut tgt.function_eval_overflow,
        );
        std::mem::swap(
            &mut self.comptime_for_overflow,
            &mut tgt.comptime_for_overflow,
        );
        std::mem::swap(&mut self.function_call_stack, &mut tgt.function_call_stack);
        std::mem::swap(&mut self.function_effects, &mut tgt.function_effects);
        std::mem::swap(
            &mut self.function_effect_stack,
            &mut tgt.function_effect_stack,
        );
        std::mem::swap(&mut self.converting_funcs, &mut tgt.converting_funcs);
        std::mem::swap(&mut self.errors, &mut tgt.errors);
        std::mem::swap(&mut self.namespaces, &mut tgt.namespaces);
        self.disalbe_const_opt = tgt.disalbe_const_opt;
        self.in_generic = tgt.in_generic;
        self.allow_component_as_factor = tgt.allow_component_as_factor;
        self.config = tgt.config.clone();
        self.profiler = tgt.profiler.clone();
        self.project_name = tgt.project_name.clone();
    }

    pub fn set_project_name(&mut self, project_name: &str) {
        self.project_name = Some(project_name.to_string());
    }

    pub fn project_name(&self) -> Option<&str> {
        self.project_name.as_deref()
    }

    pub fn enable_conv_profiler(&mut self) {
        if std::env::var("VERYL_CONV_PROFILE").is_ok() {
            self.profiler = Some(Arc::new(Mutex::new(ConvProfile::default())));
        }
    }

    pub fn conv_profile_guard(&self, component_name: StrId) -> Option<ConvProfileGuard> {
        self.profiler.as_ref().map(|p| {
            let key = format!(
                "{}::{}",
                self.project_name.as_deref().unwrap_or("unknown"),
                component_name
            );
            ConvProfileGuard::new(Arc::clone(p), key)
        })
    }

    pub fn finalize_conv_profiler(&self) -> Result<()> {
        if let Some(profiler) = self.profiler.as_ref().map(|p| p.lock().unwrap()) {
            profiler.to_toml_file(&std::env::var("VERYL_CONV_PROFILE").unwrap())
        } else {
            Ok(())
        }
    }

    pub fn get_override(&self, x: &VarPath) -> Option<&(Comptime, Expression)> {
        let overrides = self.overrides.last()?;
        overrides.get(x)
    }

    pub fn get_variable_info(&self, id: VarId) -> Option<VariableInfo> {
        self.variables.get(&id).map(VariableInfo::new)
    }

    pub(crate) fn begin_function_effect(
        &mut self,
        path: FuncPath,
        outputs: HashMap<VarId, FunctionOutputBinding>,
        effect: RuntimeFunctionEffect,
    ) {
        self.function_effect_stack.push(FunctionEffectFrame {
            path,
            outputs,
            effect,
        });
    }

    pub(crate) fn finish_function_effect(&mut self) {
        if let Some(frame) = self.function_effect_stack.pop() {
            self.function_effects.insert(frame.path, frame.effect);
        }
    }

    pub(crate) fn function_effect(&self, path: &FuncPath) -> Option<RuntimeFunctionEffect> {
        self.function_effects.get(path).cloned()
    }

    pub(crate) fn record_function_call_effect(
        &mut self,
        effect: &RuntimeFunctionEffect,
        outputs: &[(VarPath, Vec<crate::ir::AssignDestination>)],
    ) {
        let Some(frame) = self.function_effect_stack.last() else {
            return;
        };
        let caller_outputs = frame.outputs.clone();
        let mut external_write = effect.external_write;
        let mut written_outputs = HashSet::default();

        for (formal, destinations) in outputs {
            for written in effect.written_paths_for(formal) {
                let suffix = &written.0[formal.0.len()..];
                for destination in destinations {
                    if let Some(output) = caller_outputs.get(&destination.id) {
                        let mut mapped = destination.path.clone();
                        mapped.0.extend_from_slice(suffix);
                        written_outputs.insert(mapped);
                        external_write |= output.scheduler_external;
                    } else if self
                        .get_variable_info(destination.id)
                        .is_none_or(|variable| variable.affiliation != Affiliation::Function)
                    {
                        external_write = true;
                    }
                }
            }
        }

        if let Some(frame) = self.function_effect_stack.last_mut() {
            frame.effect.external_write |= external_write;
            frame.effect.written_outputs.extend(written_outputs);
        }
    }

    pub(crate) fn record_function_statement_writes(&mut self, statements: &[Statement]) {
        let Some(caller_outputs) = self
            .function_effect_stack
            .last()
            .map(|frame| frame.outputs.clone())
        else {
            return;
        };
        let mut effect = RuntimeFunctionEffect::default();

        fn visit(
            context: &Context,
            statements: &[Statement],
            caller_outputs: &HashMap<VarId, FunctionOutputBinding>,
            effect: &mut RuntimeFunctionEffect,
        ) {
            for statement in statements {
                match statement {
                    Statement::Assign(assign) => {
                        for destination in &assign.dst {
                            if let Some(output) = caller_outputs.get(&destination.id) {
                                effect.written_outputs.insert(destination.path.clone());
                                effect.external_write |= output.scheduler_external;
                            } else if context.get_variable_info(destination.id).is_none_or(
                                |variable| {
                                    variable.affiliation != Affiliation::Function
                                        || matches!(variable.kind, VarKind::Output | VarKind::Inout)
                                },
                            ) {
                                effect.external_write = true;
                            }
                        }
                    }
                    Statement::If(statement) => {
                        visit(context, &statement.true_side, caller_outputs, effect);
                        visit(context, &statement.false_side, caller_outputs, effect);
                    }
                    Statement::IfReset(statement) => {
                        visit(context, &statement.true_side, caller_outputs, effect);
                        visit(context, &statement.false_side, caller_outputs, effect);
                    }
                    Statement::Case(statement) => {
                        for arm in &statement.arms {
                            visit(context, &arm.body, caller_outputs, effect);
                        }
                        visit(context, &statement.default, caller_outputs, effect);
                    }
                    Statement::For(statement) => {
                        visit(context, &statement.body, caller_outputs, effect);
                    }
                    Statement::SystemFunctionCall(_)
                    | Statement::FunctionCall(_)
                    | Statement::TbMethodCall(_)
                    | Statement::Break
                    | Statement::Unsupported(_)
                    | Statement::Null => {}
                }
            }
        }

        visit(self, statements, &caller_outputs, &mut effect);
        if let Some(frame) = self.function_effect_stack.last_mut() {
            frame.effect.external_write |= effect.external_write;
            frame.effect.written_outputs.extend(effect.written_outputs);
        }
    }

    pub fn resolve_path(&self, mut path: GenericSymbolPath) -> GenericSymbolPath {
        let Some((scope, define_context)) = scope::token_scope(path.paths[0].base.id) else {
            return path;
        };
        path.resolve_imported(scope, &define_context, None);
        for map in self.generic_maps.iter().rev() {
            path.apply_map(map);
        }
        path
    }

    pub fn insert_var_path(&mut self, path: VarPath, value: Comptime) -> VarId {
        let id = self.var_id;
        self.insert_var_path_with_id(path, id, value);
        self.var_id.inc();
        id
    }

    pub fn insert_var_path_with_id(&mut self, path: VarPath, id: VarId, value: Comptime) {
        if let Some(x) = self.hierarchical_variables.last_mut() {
            x.push(path.clone());
        }

        let shadowed = self.var_paths.insert(path.clone(), (id, value));

        // store variable shadowed by inner-scope variable
        if let Some(x) = shadowed {
            self.shadowed_variables
                .entry(path)
                .and_modify(|v| v.push(x.clone()))
                .or_insert(vec![x]);
        }
    }

    pub fn insert_func_path(&mut self, path: FuncPath) -> VarId {
        let id = self.var_id;
        self.func_paths.insert(path, id);
        self.var_id.inc();
        id
    }

    pub fn insert_variable(&mut self, id: VarId, mut variable: Variable) {
        if self.ignore_var_func {
            return;
        }

        let hier = &self.hierarchy;
        if !hier.is_empty() {
            variable.path.add_prelude(hier);
        }
        self.variables.insert(id, variable);
    }

    pub fn insert_port_type(&mut self, path: VarPath, r#type: Type, clock_domain: ClockDomain) {
        self.port_types.insert(path, (r#type, clock_domain));
    }

    pub fn insert_function(&mut self, id: VarId, mut function: Function) {
        if self.ignore_var_func {
            return;
        }

        let hier = &self.hierarchy;
        if !hier.is_empty() {
            function.path.add_prelude(hier);
        }
        self.functions.insert(id, function);
    }

    pub fn insert_declaration(&mut self, decl: Declaration) {
        if !decl.is_null() {
            self.declarations.push(decl);
        }
    }

    pub fn insert_error(&mut self, mut error: AnalyzerError) {
        let mut replaced = false;

        // merge MultipleAssignment which have same identifier
        if let AnalyzerError::MultipleAssignment {
            identifier: ref new_ident,
            error_locations: ref mut new_locations,
            ..
        } = error
        {
            for e in &mut self.errors {
                if let AnalyzerError::MultipleAssignment {
                    identifier: org_ident,
                    error_locations: org_locations,
                    ..
                } = e
                    && new_ident == org_ident
                {
                    org_locations.append(new_locations);
                    org_locations.sort();
                    org_locations.dedup();
                    replaced = true;
                    break;
                }
            }
        }

        if !replaced && !self.errors.contains(&error) {
            self.errors.push(error);
        }
    }

    pub fn insert_modport(&mut self, name: StrId, members: Vec<(StrId, Direction)>) {
        self.modports.insert(name, members);
    }

    pub fn extract_function(&mut self, context: &mut Context, base: &VarPath, array: &ShapeRef) {
        for (id, mut variable) in context.variables.drain() {
            let path_offset = variable.path.0.len();
            variable.path.add_prelude(&base.0);
            variable.prepend_array_at_path(array, path_offset);
            self.variables.insert(id, variable);
        }

        for (mut path, id) in context.func_paths.drain() {
            if !path.path.starts_with(&base.0) {
                path.path.add_prelude(&base.0);
            }
            self.func_paths.insert(path, id);
        }

        for (id, mut function) in context.functions.drain() {
            if !array.is_empty() {
                let total_array = array.total();
                let func_body = function.functions.remove(0);
                if let Some(total_array) = total_array {
                    for i in 0..total_array {
                        let var_index = VarIndex::from_index(i, array);
                        let mut func_body = func_body.clone();
                        func_body.set_index(&var_index);
                        function.functions.push(func_body);
                    }
                }
            }

            if !function.path.path.starts_with(&base.0) {
                function.path.path.add_prelude(&base.0);
            }
            self.functions.insert(id, function);
        }
    }

    pub fn extract_var_paths(&mut self, context: &Context, base: &VarPath, array: &ShapeRef) {
        for (path, (id, comptime)) in &context.var_paths {
            if path.starts_with(&base.0) {
                let mut path = path.clone();
                path.remove_prelude(&base.0);
                if !path.0.is_empty() {
                    let mut comptime = comptime.clone();
                    for _ in 0..array.dims() {
                        comptime.r#type.array.remove(0);
                    }
                    self.var_paths.insert(path, (*id, comptime));
                }
            }
        }
    }

    pub fn extract_interface_member(
        &mut self,
        base: StrId,
        array: &ShapeRef,
        component: Interface,
        modport: Option<StrId>,
        clock_domain: ClockDomain,
        token: TokenRange,
    ) {
        let mut inserted = HashSet::default();

        let modport_members = if let Some(x) = &modport {
            component.get_modport(x)
        } else {
            HashMap::default()
        };

        let mut id_map = HashMap::default();
        for mut variable in component.variables.into_values() {
            if modport.is_some() {
                if let Some(x) = modport_members.get(&variable.path.first()) {
                    variable.kind = match x {
                        Direction::Input => VarKind::Input,
                        Direction::Output => VarKind::Output,
                        Direction::Inout => VarKind::Inout,
                        _ => variable.kind,
                    };
                } else {
                    // Skip non-modport member
                    continue;
                }
            }

            let path_offset = variable.path.0.len();
            variable.prepend_array_at_path(array, path_offset);

            // override token, affiliation to interface instance
            variable.token = token;
            variable.affiliation = self.get_affiliation();

            inserted.insert(variable.path.clone());
            let comptime = Comptime::from_type(variable.r#type.clone(), clock_domain, token);
            variable.path.add_prelude(&[base]);
            let id = self.insert_var_path(variable.path.clone(), comptime);

            // id mapping for struct/union members
            id_map.insert(variable.id, id);

            variable.id = id;
            self.insert_variable(id, variable);
        }

        // import non-variable VarPath
        for (mut path, (id, mut comptime)) in component.var_paths {
            if !inserted.contains(&path) {
                path.add_prelude(&[base]);
                comptime.r#type.prepend_array(array);

                if let Some(id) = id_map.get(&id) {
                    self.insert_var_path_with_id(path, *id, comptime);
                } else {
                    self.insert_var_path(path, comptime);
                }
            }
        }
    }

    pub fn inc_select_dim(&mut self) {
        if let Some(x) = self.select_dims.last_mut() {
            *x += 1;
        }
    }

    pub fn get_select_dim(&self) -> Option<usize> {
        self.select_dims.last().copied()
    }

    pub fn set_default_clock(&mut self, path: VarPath, id: SymbolId) {
        self.default_clock.replace((path, id));
    }

    pub fn set_default_reset(&mut self, path: VarPath, id: SymbolId) {
        self.default_reset.replace((path, id));
    }

    pub fn get_default_clock(&self) -> Option<(FfClock, SymbolId)> {
        if let Some(x) = &self.default_clock {
            if let Some((id, comptime)) = &self.var_paths.get(&x.0) {
                let ret = FfClock {
                    id: *id,
                    index: VarIndex::default(),
                    select: VarSelect::default(),
                    comptime: comptime.clone(),
                };
                Some((ret, x.1))
            } else {
                None
            }
        } else {
            None
        }
    }

    pub fn get_default_reset(&self) -> Option<(FfReset, SymbolId)> {
        if let Some(x) = &self.default_reset {
            if let Some((id, comptime)) = &self.var_paths.get(&x.0) {
                let ret = FfReset {
                    id: *id,
                    index: VarIndex::default(),
                    select: VarSelect::default(),
                    comptime: comptime.clone(),
                };
                Some((ret, x.1))
            } else {
                None
            }
        } else {
            None
        }
    }

    pub fn insert_inst_signature(&mut self, identifier: &Identifier, sig: &Signature) {
        self.inst_signatures.insert(identifier.text(), sig.clone());
    }

    pub fn collect_modport_signatures(
        &mut self,
        inst: &ComponentInstantiation,
    ) -> HashMap<StrId, Signature> {
        let mut signatures = HashMap::default();
        if !self.inst_signatures.is_empty()
            && let Some(ref opt2) = inst.component_instantiation_opt2
            && let Some(ref port_opt) = opt2.inst_port.inst_port_opt
        {
            let port_items: Vec<_> = port_opt.inst_port_list.as_ref().into();
            for port_item in &port_items {
                let inst_name = if let Some(ref x) = port_item.inst_port_item_opt {
                    let Some(identifier) = x.expression.unwrap_identifier().map(|x| x.identifier())
                    else {
                        continue;
                    };
                    identifier.token.text
                } else {
                    port_item.identifier.text()
                };
                if let Some(sig) = self.inst_signatures.get(&inst_name) {
                    let port_name = port_item.identifier.text();
                    signatures.insert(port_name, sig.clone());
                }
            }
        }

        self.modport_signatures.push(signatures.clone());
        signatures
    }

    pub fn pop_modport_signatures(&mut self) {
        self.modport_signatures.pop();
    }

    pub fn get_modport_signature(&self, port_identifier: &Identifier) -> Option<Signature> {
        if let Some(signatures) = self.modport_signatures.last() {
            signatures.get(&port_identifier.text()).cloned()
        } else {
            None
        }
    }

    pub fn check_size(&mut self, x: usize, token: TokenRange) -> Option<usize> {
        if x > self.config.evaluate_size_limit {
            self.insert_error(AnalyzerError::exceed_limit(
                ExceedLimitKind::EvaluateSize,
                x,
                &token,
            ));
            None
        } else {
            Some(x)
        }
    }

    pub fn block<F, T>(&mut self, f: F) -> IrResult<T>
    where
        F: FnOnce(&mut Context) -> IrResult<T>,
    {
        f(self)
    }

    pub fn push_instance_history(&mut self, x: Signature) -> Result<bool, InstanceHistoryError> {
        self.instance_history.push(x, &self.config)
    }

    pub fn pop_instance_history(&mut self) {
        self.instance_history.pop();
    }

    pub fn get_current_signature(&self) -> Option<&Signature> {
        self.instance_history.get_current_signature()
    }

    pub fn get_instance_history(&self, sig: &Signature) -> Option<(Arc<Component>, bool)> {
        self.instance_history.get(sig)
    }

    pub fn set_instance_history(&mut self, sig: &Signature, component: Arc<Component>) {
        self.instance_history.set(sig, component, self.in_generic);
    }

    pub fn remove_instance_history(&mut self, sig: &Signature) {
        self.instance_history.remove(sig);
    }

    pub fn push_hierarchy(&mut self, x: StrId) {
        self.hierarchy.push(x);
        self.hierarchical_variables.push(vec![]);
        self.hierarchical_functions.push(vec![]);
    }

    /// The current generate-block prefix (e.g. `["g_leaf[0]"]`), the same prefix
    /// applied to variable paths and to `InstDeclaration::hierarchy`.
    pub fn current_hierarchy(&self) -> &[StrId] {
        &self.hierarchy
    }

    pub fn pop_hierarchy(&mut self) {
        self.hierarchy.pop();
        let drops = self.hierarchical_variables.pop();
        if let Some(drops) = drops {
            for x in drops {
                if let Some(y) = self.shadowed_variables.get_mut(&x)
                    && let Some(y) = y.pop()
                {
                    self.var_paths.insert(x, y);
                } else {
                    self.var_paths.remove(&x);
                }
            }
        }
        let drops = self.hierarchical_functions.pop();
        if let Some(drops) = drops {
            for x in drops {
                self.func_paths.remove(&x);
            }
        }
    }

    pub fn push_affiliation(&mut self, x: Affiliation) {
        self.affiliation.push(x);
    }

    pub fn pop_affiliation(&mut self) {
        self.affiliation.pop();
    }

    pub fn push_override(&mut self, x: HashMap<VarPath, (Comptime, Expression)>) {
        self.overrides.push(x);
    }

    pub fn pop_override(&mut self) {
        self.overrides.pop();
    }

    pub fn push_generic_map(&mut self, x: Vec<GenericMap>) {
        self.generic_maps.push(x);
    }

    pub fn pop_generic_map(&mut self) {
        self.generic_maps.pop();
    }

    /// Marks `id` as being expanded; returns false if it already is (a cycle).
    pub fn push_typedef_visiting(&mut self, id: SymbolId) -> bool {
        if self.typedef_visiting.contains(&id) {
            false
        } else {
            self.typedef_visiting.push(id);
            true
        }
    }

    pub fn pop_typedef_visiting(&mut self) {
        self.typedef_visiting.pop();
    }

    pub fn is_affiliated(&self, value: Affiliation) -> bool {
        if let Some(x) = self.affiliation.last() {
            *x == value
        } else {
            false
        }
    }

    pub fn get_affiliation(&self) -> Affiliation {
        self.affiliation.last().copied().unwrap()
    }

    pub fn push_namespace(&mut self, namespace: Namespace) {
        self.namespaces.push(namespace);
    }

    pub fn pop_namespace(&mut self) {
        self.namespaces.pop();
    }

    pub fn current_namespace(&self) -> Option<Namespace> {
        self.namespaces.last().cloned()
    }

    pub fn find_path(&self, path: &VarPath) -> Option<(VarId, Comptime)> {
        self.var_paths.get(path).cloned()
    }

    pub fn get_variable(&self, id: &VarId) -> Option<Variable> {
        self.variables.get(id).cloned()
    }

    pub fn remove_path(&mut self, path: &VarPath) {
        self.var_paths.remove(path);
    }

    pub fn drain_var_paths(&mut self) -> HashMap<VarPath, (VarId, Comptime)> {
        std::mem::take(&mut self.var_paths)
    }

    pub fn drain_func_paths(&mut self) -> HashMap<FuncPath, VarId> {
        std::mem::take(&mut self.func_paths)
    }

    pub fn drain_variables(&mut self) -> HashMap<VarId, Variable> {
        std::mem::take(&mut self.variables)
    }

    pub fn drain_port_types(&mut self) -> HashMap<VarPath, (Type, ClockDomain)> {
        std::mem::take(&mut self.port_types)
    }

    pub fn drain_functions(&mut self) -> HashMap<VarId, Function> {
        std::mem::take(&mut self.functions)
    }

    pub fn drain_modports(&mut self) -> HashMap<StrId, Vec<(StrId, Direction)>> {
        std::mem::take(&mut self.modports)
    }

    pub fn drain_declarations(&mut self) -> Vec<Declaration> {
        std::mem::take(&mut self.declarations)
    }

    pub fn drain_errors(&mut self) -> Vec<AnalyzerError> {
        std::mem::take(&mut self.errors)
    }
}

#[cfg(test)]
mod analysis_transaction_tests {
    use super::*;
    use crate::value::Value;

    fn test_variable(id: VarId, value: u64) -> Variable {
        Variable::new(
            id,
            VarPath::default(),
            VarKind::Variable,
            Type::default(),
            vec![Value::new(value, 1, false)],
            Affiliation::Module,
            &TokenRange::default(),
            1,
        )
    }

    fn value(context: &Context, id: VarId) -> &Value {
        &context.variables[&id].value[0]
    }

    #[test]
    fn nested_analysis_transactions_restore_each_snapshot() {
        let id = VarId::from_raw(1);
        let mut context = Context::default();
        context.variables.insert(id, test_variable(id, 0));

        context.begin_analysis_transaction();
        context.variable_mut(&id).unwrap().value[0] = Value::new(1, 1, false);
        context.begin_analysis_transaction();
        context.variable_mut(&id).unwrap().value[0] = Value::new(0, 1, false);

        context.rollback_analysis_transaction();
        assert_eq!(value(&context, id), &Value::new(1, 1, false));
        context.rollback_analysis_transaction();
        assert_eq!(value(&context, id), &Value::new(0, 1, false));
    }

    #[test]
    fn reused_analysis_transactions_do_not_leak_variable_state() {
        let id = VarId::from_raw(1);
        let mut context = Context::default();
        context.variables.insert(id, test_variable(id, 0));

        for _ in 0..2 {
            context.begin_analysis_transaction();
            assert_eq!(value(&context, id), &Value::new(0, 1, false));
            context.variable_mut(&id).unwrap().value[0] = Value::new(1, 1, false);
            context.rollback_analysis_transaction();
        }
        assert_eq!(value(&context, id), &Value::new(0, 1, false));
    }
}