pine-sema 0.2.2

Semantic analysis for Pine Script.
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
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
//! The semantic analyzer: a scope-aware walk that emits Tier 1 (name
//! resolution) and Tier 4 (structural) errors.
//!
//! This intentionally does **not** use the shared [`pine_ast::Visitor`]. That
//! traversal is for observational passes; sema needs to push/pop a scope at
//! every block boundary, hoist declarations, and track context (loop depth,
//! global-vs-local), which the default recurse-everything walk doesn't express.
//! So we hand-write the recursion and interleave the scope bookkeeping.

use std::collections::{HashMap, HashSet};

use pine_ast::{Argument, ExportItem, Expr, FunctionParam, Literal, Loc, Program, Stmt};
use pine_core::{LibraryLoader, PineOutput};
use pine_interpreter::{BuiltinSignature, Value};
use pine_parser::Parser;

use crate::scope::{is_global_only, Namespace, SymbolKind};
use crate::symbols::{FileId, ScopeId, ScopeKind, Symbol, SymbolId, SymbolTable};
use pine_diagnostics::Diagnostic;

pub struct Analyzer<'a, O: PineOutput> {
    diagnostics: Vec<Diagnostic>,
    /// Enclosing loops in the current function (reset at function boundaries).
    loop_depth: u32,
    /// The runtime's registered built-ins (namespaces, globals, per-bar variables).
    builtins: &'a HashMap<String, Value<O>>,
    /// Script declarations seen (indicator/strategy/library); at most one allowed.
    declarations: u32,
    /// Whether the current file declared `library(...)` — required of an import.
    library_declared: bool,
    /// Free functions, `name -> (required, total)` param counts, for arity checks.
    functions: HashMap<String, (usize, usize)>,
    /// User type/enum names, collected up front for forward-referencing annotations.
    user_types: HashSet<String>,
    /// Functions enclosing the current point; the last is the caller of any call.
    fn_stack: Vec<String>,
    /// The call graph `(caller, callee, call-site)`, scanned afterwards for cycles.
    call_edges: Vec<CallEdge>,
    /// The durable symbol table; `scope_ids` tracks the current (innermost-last) scope.
    symbols: SymbolTable,
    scope_ids: Vec<ScopeId>,
    /// Resolves `import` paths to source; absent means no cross-file resolution.
    loader: Option<&'a dyn LibraryLoader>,
}

/// Per-file state saved and restored around analyzing a library.
struct FileState {
    scope_ids: Vec<ScopeId>,
    loop_depth: u32,
    functions: HashMap<String, (usize, usize)>,
    user_types: HashSet<String>,
    declarations: u32,
    library_declared: bool,
    fn_stack: Vec<String>,
    call_edges: Vec<CallEdge>,
}

/// One call-graph edge: `(caller, callee, call-site location)`.
type CallEdge = (String, String, Loc);

/// The script-declaration functions — a script must have exactly one.
const SCRIPT_DECLARATIONS: &[&str] = &["study", "indicator", "strategy", "library"];

/// Built-in type names an annotation may use without a user declaration.
const BUILTIN_TYPES: &[&str] = &[
    "int",
    "float",
    "bool",
    "string",
    "color",
    "line",
    "linefill",
    "label",
    "box",
    "table",
    "polyline",
    "array",
    "matrix",
    "map",
    "footprint",
    "volume_row",
];

/// The called name as written, for diagnostics: `plot` or `ta.sma`.
fn callee_name(callee: &Expr) -> String {
    match callee {
        Expr::Variable { name, .. } => name.clone(),
        Expr::MemberAccess { object, member, .. } => match object.as_ref() {
            Expr::Variable {
                name: namespace, ..
            } => format!("{namespace}.{member}"),
            _ => member.clone(),
        },
        _ => String::new(),
    }
}

/// Whether `start` reaches `target` in the call graph (a self-call counts).
fn reaches(start: &str, target: &str, adjacency: &HashMap<&str, Vec<&str>>) -> bool {
    if start == target {
        return true;
    }
    let mut stack = vec![start];
    let mut seen = HashSet::new();
    while let Some(node) = stack.pop() {
        if !seen.insert(node) {
            continue;
        }
        if let Some(callees) = adjacency.get(node) {
            for &callee in callees {
                if callee == target {
                    return true;
                }
                stack.push(callee);
            }
        }
    }
    false
}

/// The type names within an annotation: the base and every generic argument,
/// with `[]`/`<>`/`,` stripped (`map<string, Point>` -> `map`, `string`, `Point`).
fn type_names(annotation: &str) -> impl Iterator<Item = &str> {
    annotation
        .split(['<', '>', ',', '[', ']', ' '])
        .filter(|name| !name.is_empty())
}

/// How to name a literal's type in a diagnostic.
fn describe_literal(literal: &Literal) -> &'static str {
    match literal {
        Literal::Int(_) | Literal::Number(_) => "a number",
        Literal::String(_) => "a string",
        Literal::Bool(_) => "a bool",
        Literal::HexColor(_) => "a color",
        Literal::Na => "na",
    }
}

impl<'a, O: PineOutput> Analyzer<'a, O> {
    pub fn new(
        builtins: &'a HashMap<String, Value<O>>,
        loader: Option<&'a dyn LibraryLoader>,
    ) -> Self {
        Self {
            diagnostics: Vec::new(),
            loop_depth: 0,
            builtins,
            declarations: 0,
            library_declared: false,
            functions: HashMap::new(),
            user_types: HashSet::new(),
            fn_stack: Vec::new(),
            call_edges: Vec::new(),
            symbols: SymbolTable::new(),
            scope_ids: vec![SymbolTable::GLOBAL],
            loader,
        }
    }

    /// The innermost open scope — where names resolve from and declarations are
    /// recorded into.
    fn current_scope(&self) -> ScopeId {
        *self.scope_ids.last().expect("scope stack is never empty")
    }

    fn current_file(&self) -> FileId {
        self.symbols.scope_file(self.current_scope())
    }

    fn current_lib(&self) -> Option<String> {
        let file = self.current_file();
        (file != SymbolTable::MAIN).then(|| self.symbols.file_path(file).to_string())
    }

    /// Open a nested scope in the symbol tree and make it current.
    fn enter_scope(&mut self, kind: ScopeKind) {
        let child = self.symbols.open_scope(self.current_scope(), kind);
        self.scope_ids.push(child);
    }

    /// Close the current scope; its symbols stay in the table as a child scope.
    fn exit_scope(&mut self) {
        self.scope_ids.pop();
    }

    /// Declare a symbol, stamped with the current file.
    fn record(&mut self, mut symbol: Symbol) -> SymbolId {
        symbol.file = self.current_file();
        self.symbols.declare(symbol)
    }

    /// Resolve `name` from the current scope outward.
    fn resolve(&self, name: &str) -> Option<SymbolKind> {
        self.symbols
            .resolve(self.current_scope(), name)
            .map(|symbol| symbol.kind)
    }

    /// Record a use of `name`, if it resolves to a user symbol.
    fn record_use(&mut self, name: &str, loc: Loc) {
        let scope = self.current_scope();
        if let Some(id) = self.symbols.resolve_id(scope, name) {
            let file = self.current_file();
            self.symbols.record_use(file, loc.position(), id);
        }
    }

    /// A declaration's user type, from an annotation or a `Type.new()` initializer.
    fn infer_var_type(
        &self,
        type_annotation: Option<&String>,
        initializer: Option<&Expr>,
    ) -> Option<SymbolId> {
        if let Some(annotation) = type_annotation {
            let base = annotation.trim_end_matches("[]");
            if let Some(id) = self.symbols.resolve_id(self.current_scope(), base) {
                if matches!(
                    self.symbols.symbol(id).kind,
                    SymbolKind::Type | SymbolKind::Enum
                ) {
                    return Some(id);
                }
            }
        }
        // A `Type.new(...)` (or `lib.Type.new(...)`) constructor initializer.
        if let Some(Expr::Call { callee, .. }) = initializer {
            if let Expr::MemberAccess { object, member, .. } = callee.as_ref() {
                if member == "new" {
                    if let Some(id) = self.expr_type(object) {
                        if self.symbols.symbol(id).kind == SymbolKind::Type {
                            return Some(id);
                        }
                    }
                }
            }
        }
        None
    }

    /// The type/enum a symbol denotes: itself, or a variable's `type_ref`.
    fn owner_type(&self, id: SymbolId) -> Option<SymbolId> {
        let symbol = self.symbols.symbol(id);
        match symbol.kind {
            SymbolKind::Type | SymbolKind::Enum => Some(id),
            SymbolKind::Var => symbol.type_ref,
            _ => None,
        }
    }

    /// The type/enum an expression's member access reads from (`Enum.Case`,
    /// `v.field`, `lib.Point`), or `None` when the type is unknown.
    fn expr_type(&self, expr: &Expr) -> Option<SymbolId> {
        let id = match expr {
            Expr::Variable { name, .. } => self.symbols.resolve_id(self.current_scope(), name)?,
            Expr::MemberAccess { object, member, .. } => self.resolve_member(object, member)?,
            _ => return None,
        };
        self.owner_type(id)
    }

    /// The declaration `object.member` points at: a library export, or a member
    /// of the object's user type.
    fn resolve_member(&self, object: &Expr, member: &str) -> Option<SymbolId> {
        if let Some(module) = self.alias_module(object) {
            return self.symbols.exported_id(module, member);
        }
        let owner = self.expr_type(object)?;
        self.symbols.member_id(owner, member)
    }

    /// `object.member` where the object's full member set is known — a builtin
    /// namespace or a resolved import — yet `member` is not among them. False
    /// whenever the members can't be enumerated (a user variable, an unloaded
    /// import), so no diagnostic is invented on incomplete information.
    fn unknown_member(&self, object: &Expr, member: &str) -> bool {
        // A resolved import alias: its export set is fully known.
        if let Some(module) = self.alias_module(object) {
            return self.symbols.exported_id(module, member).is_none();
        }
        // A builtin namespace object, not shadowed by a user declaration. Sema
        // reads the same registry the interpreter calls into, so an absent
        // field is exactly one a run would reject.
        if let Expr::Variable { name, .. } = object {
            if self.resolve(name).is_none() {
                if let Some(Value::Object { fields, .. }) = self.builtins.get(name) {
                    return !fields.borrow().contains_key(member);
                }
            }
        }
        false
    }

    /// The imported library's global scope, if `object` is an import alias.
    fn alias_module(&self, object: &Expr) -> Option<ScopeId> {
        let Expr::Variable { name, .. } = object else {
            return None;
        };
        let id = self.symbols.resolve_id(self.current_scope(), name)?;
        let symbol = self.symbols.symbol(id);
        (symbol.kind == SymbolKind::Import)
            .then_some(symbol.module)
            .flatten()
    }

    fn is_builtin(&self, name: &str) -> bool {
        self.builtins.contains_key(name)
    }

    /// The signature of the builtin the callee names (`plot` or `ta.sma`), or
    /// `None` — a user-shadowed name or a builtin with no declared parameters.
    fn builtin_signature(&self, callee: &Expr) -> Option<&'static BuiltinSignature> {
        let value = match callee {
            Expr::Variable { name, .. } => {
                if self.resolve(name).is_some() {
                    return None;
                }
                self.builtins.get(name)?.clone()
            }
            Expr::MemberAccess { object, member, .. } => {
                let Expr::Variable {
                    name: namespace, ..
                } = object.as_ref()
                else {
                    return None;
                };
                if self.resolve(namespace).is_some() {
                    return None;
                }
                match self.builtins.get(namespace)? {
                    Value::Object { fields, .. } => fields.borrow().get(member)?.clone(),
                    _ => return None,
                }
            }
            _ => return None,
        };

        match value {
            Value::BuiltinFunction(builtin) if !builtin.signature.params.is_empty() => {
                Some(builtin.signature)
            }
            // A callable namespace object like `plot(...)` carries its `Builtin`.
            Value::Object {
                call: Some(builtin),
                ..
            } if !builtin.signature.params.is_empty() => Some(builtin.signature),
            _ => None,
        }
    }

    /// Check a call against the builtin's parameters: too many/few arguments, an
    /// unknown named argument, and a literal of the wrong type.
    fn check_builtin_args(
        &mut self,
        name: &str,
        signature: &BuiltinSignature,
        args: &[Argument],
        loc: Loc,
    ) {
        let positional = args
            .iter()
            .filter(|arg| matches!(arg, Argument::Positional(_)))
            .count();

        if let Some(max) = signature.max_positional() {
            if positional > max {
                self.emit(
                    "too-many-arguments",
                    loc,
                    format!("`{name}` takes at most {max} arguments, found {positional}"),
                );
            }
        }

        let mut index = 0;
        for arg in args {
            let (param, value) = match arg {
                Argument::Positional(value) => {
                    let param = signature.positional(index);
                    index += 1;
                    (param, value)
                }
                Argument::Named { name: label, value } => match signature.named(label) {
                    Some(param) => (Some(param), value),
                    None => {
                        self.emit(
                            "unknown-argument",
                            loc,
                            format!("`{name}` has no argument named `{label}`"),
                        );
                        continue;
                    }
                },
            };

            // Only a literal's type is known without inference; anything else
            // is left to the runtime.
            let (Some(param), Expr::Literal(literal)) = (param, value) else {
                continue;
            };
            if !param.ty.accepts(literal) {
                let found = describe_literal(literal);
                let expected = param.ty.describe();
                let label = param.name.clone();
                self.emit(
                    "argument-type",
                    loc,
                    format!("`{name}` expects {expected} for `{label}`, found {found}"),
                );
            }
        }

        // Counting (not position-matching) required params stays sound for
        // leading-optional overloads like `ta.highest(length)`.
        let required = signature
            .params
            .iter()
            .filter(|param| param.required)
            .count();
        if args.len() < required {
            self.emit(
                "too-few-arguments",
                loc,
                format!(
                    "`{name}` requires at least {required} arguments, found {}",
                    args.len()
                ),
            );
        }
    }

    /// Analyze one file in its own scope, type set, and call graph.
    fn run_file(&mut self, program: &Program) {
        // Types may be referenced before their declaration, so collect them first.
        for stmt in &program.statements {
            match stmt {
                Stmt::TypeDecl { name, .. } | Stmt::EnumDecl { name, .. } => {
                    self.user_types.insert(name.clone());
                }
                _ => {}
            }
        }
        for stmt in &program.statements {
            self.check_stmt(stmt);
        }
        self.detect_recursion();
    }

    /// Swap in fresh state for a library, returning the caller's to restore.
    fn enter_file(&mut self, root: ScopeId) -> FileState {
        FileState {
            scope_ids: std::mem::replace(&mut self.scope_ids, vec![root]),
            loop_depth: std::mem::take(&mut self.loop_depth),
            functions: std::mem::take(&mut self.functions),
            user_types: std::mem::take(&mut self.user_types),
            declarations: std::mem::take(&mut self.declarations),
            library_declared: std::mem::take(&mut self.library_declared),
            fn_stack: std::mem::take(&mut self.fn_stack),
            call_edges: std::mem::take(&mut self.call_edges),
        }
    }

    fn exit_file(&mut self, saved: FileState) {
        self.scope_ids = saved.scope_ids;
        self.loop_depth = saved.loop_depth;
        self.functions = saved.functions;
        self.user_types = saved.user_types;
        self.declarations = saved.declarations;
        self.library_declared = saved.library_declared;
        self.fn_stack = saved.fn_stack;
        self.call_edges = saved.call_edges;
    }

    /// Analyze the library at `path` once. Registering it before the walk lets a
    /// re-entrant import find it, which breaks cycles.
    fn resolve_import(&mut self, path: &str, loc: Loc) -> Option<(FileId, ScopeId)> {
        if let Some(file) = self.symbols.file_by_path(path) {
            return Some((file, self.symbols.file_root(file)));
        }
        let loader = self.loader?;
        let source = match loader.load_library(path) {
            Ok(source) => source,
            Err(err) => {
                self.emit(
                    "import-error",
                    loc,
                    format!("cannot load library `{path}`: {err}"),
                );
                return None;
            }
        };
        let program = match Parser::parse_source(&source) {
            Ok(program) => program,
            Err(err) => {
                self.emit(
                    "import-parse-error",
                    loc,
                    format!("cannot parse library `{path}`: {err}"),
                );
                return None;
            }
        };
        let (file, root) = self.symbols.add_file(path);
        let saved = self.enter_file(root);
        self.run_file(&program);
        let is_library = self.library_declared;
        self.exit_file(saved);
        // Reported back in the importing file, at the `import` statement.
        if !is_library {
            self.emit(
                "not-a-library",
                loc,
                format!("imported script `{path}` has no `library()` declaration"),
            );
        }
        Some((file, root))
    }

    /// Analyze a whole program, returning the errors found.
    pub fn analyze(mut self, program: &Program) -> Vec<Diagnostic> {
        self.run_file(program);
        self.diagnostics
    }

    /// Analyze a whole program, returning both the errors and the symbol table
    /// reconstructed from the same walk.
    pub fn into_analysis(mut self, program: &Program) -> (Vec<Diagnostic>, SymbolTable) {
        self.run_file(program);
        (self.diagnostics, self.symbols)
    }

    /// Report call-graph cycles as recursion (Pine forbids it), at the call site.
    fn detect_recursion(&mut self) {
        let cycles: Vec<CallEdge> = {
            let mut adjacency: HashMap<&str, Vec<&str>> = HashMap::new();
            for (caller, callee, _) in &self.call_edges {
                adjacency.entry(caller).or_default().push(callee);
            }
            self.call_edges
                .iter()
                .filter(|(caller, callee, _)| reaches(callee, caller, &adjacency))
                .cloned()
                .collect()
        };
        for (caller, callee, pos) in cycles {
            let message = if caller == callee {
                format!("`{caller}` calls itself; Pine does not allow recursion")
            } else {
                format!("`{caller}` and `{callee}` call each other; Pine does not allow recursion")
            };
            self.emit("recursion", pos, message);
        }
    }

    fn emit(&mut self, rule: &'static str, loc: Loc, message: impl Into<String>) {
        self.diagnostics
            .push(Diagnostic::error(rule, loc.position(), message).in_file(self.current_lib()));
    }

    fn warn(&mut self, rule: &'static str, loc: Loc, message: impl Into<String>) {
        self.diagnostics
            .push(Diagnostic::warning(rule, loc.position(), message).in_file(self.current_lib()));
    }

    /// Warn that a declaration shadows a built-in (Pine allows it, but warns).
    fn check_shadow(&mut self, name: &str, loc: Loc) {
        if self.is_builtin(name) {
            self.warn(
                "shadows-builtin",
                loc,
                format!("declaration of `{name}` shadows a built-in"),
            );
        }
    }

    /// Reject a type annotation naming a type that is neither a built-in nor a
    /// declared type — including every name inside a generic like `array<Foo>`.
    fn check_type_annotation(&mut self, annotation: Option<&String>, loc: Loc) {
        let Some(annotation) = annotation else {
            return;
        };
        for name in type_names(annotation) {
            if !BUILTIN_TYPES.contains(&name) && !self.user_types.contains(name) {
                self.emit("unknown-type", loc, format!("unknown type `{name}`"));
                return;
            }
        }
    }

    /// Check a user-function call's argument count against its parameters.
    fn check_call_arity(
        &mut self,
        name: &str,
        supplied: usize,
        required: usize,
        total: usize,
        loc: Loc,
    ) {
        if supplied < required {
            self.emit(
                "too-few-arguments",
                loc,
                format!("`{name}` requires at least {required} arguments, found {supplied}"),
            );
        } else if supplied > total {
            self.emit(
                "too-many-arguments",
                loc,
                format!("`{name}` takes at most {total} arguments, found {supplied}"),
            );
        }
    }

    /// Record a user function and walk its body. Declared before the body so a
    /// self-call inside reads as recursion.
    fn analyze_function(
        &mut self,
        name: &str,
        loc: Loc,
        params: &[FunctionParam],
        body: &[Stmt],
    ) -> SymbolId {
        let scope = self.current_scope();
        if self
            .symbols
            .declared_locally_in(scope, name, Namespace::Value)
        {
            self.emit(
                "duplicate-declaration",
                loc,
                format!("`{name}` is already declared in this scope"),
            );
        }
        let id = self.record(
            Symbol::new(name, SymbolKind::Function, loc.position(), scope)
                .with_params(params.iter().map(|p| p.name.clone()).collect()),
        );
        // A parameter with a default may be omitted, so it is not required.
        let required = params.iter().filter(|p| p.default_value.is_none()).count();
        self.functions
            .insert(name.to_string(), (required, params.len()));
        for param in params {
            self.check_type_annotation(param.type_annotation.as_ref(), param.loc);
        }
        self.fn_stack.push(name.to_string());
        self.function_body(
            params.iter().map(|p| {
                (
                    p.name.as_str(),
                    p.default_value.as_ref(),
                    p.loc,
                    p.type_annotation.as_ref(),
                )
            }),
            body,
        );
        self.fn_stack.pop();
        id
    }

    /// Declare `name` in the current scope, reporting a same-scope duplicate.
    fn declare(&mut self, name: &str, kind: SymbolKind, loc: Loc) -> SymbolId {
        let scope = self.current_scope();
        if self
            .symbols
            .declared_locally_in(scope, name, kind.namespace())
        {
            self.emit(
                "duplicate-declaration",
                loc,
                format!("`{name}` is already declared in this scope"),
            );
        }
        self.record(Symbol::new(name, kind, loc.position(), scope))
    }

    /// Visit a non-loop nested block (an `if`/`else` branch) in its own scope.
    fn block(&mut self, body: &[Stmt]) {
        self.enter_scope(ScopeKind::Block);
        for stmt in body {
            self.check_stmt(stmt);
        }
        self.exit_scope();
    }

    /// Visit a loop body with `loop_depth` raised so `break`/`continue` are legal.
    fn loop_body(&mut self, body: &[Stmt]) {
        self.loop_depth += 1;
        for stmt in body {
            self.check_stmt(stmt);
        }
        self.loop_depth -= 1;
    }

    /// Visit a function body in a fresh scope with `params` bound.
    fn function_body<'p>(
        &mut self,
        params: impl Iterator<Item = (&'p str, Option<&'p Expr>, Loc, Option<&'p String>)>,
        body: &[Stmt],
    ) {
        self.enter_scope(ScopeKind::Function);
        let saved_loop_depth = self.loop_depth;
        self.loop_depth = 0;
        let scope = self.current_scope();
        for (name, default, loc, type_annotation) in params {
            if let Some(default) = default {
                self.check_expr(default);
            }
            self.check_shadow(name, loc);
            if self.symbols.declared_locally(scope, name) {
                self.emit(
                    "duplicate-parameter",
                    loc,
                    format!("parameter `{name}` is declared more than once"),
                );
            }
            self.record(
                Symbol::new(name, SymbolKind::Var, loc.position(), scope)
                    .with_type(type_annotation.cloned()),
            );
        }
        for stmt in body {
            self.check_stmt(stmt);
        }
        self.loop_depth = saved_loop_depth;
        self.exit_scope();
    }

    fn check_stmt(&mut self, stmt: &Stmt) {
        match stmt {
            Stmt::VarDecl {
                name,
                initializer,
                type_annotation,
                loc,
                ..
            } => {
                self.check_type_annotation(type_annotation.as_ref(), *loc);
                self.check_shadow(name, *loc);
                if let Some(Expr::Function { params, body }) = initializer {
                    // A named function `f(x) => …`, lowered to a lambda-valued var.
                    self.analyze_function(name, *loc, params, body);
                } else {
                    // Check the initializer *before* declaring the name, so a
                    // self-reference (`x = x`) resolves against the outer scope.
                    if let Some(init) = initializer {
                        self.check_expr(init);
                    }
                    let scope = self.current_scope();
                    if self
                        .symbols
                        .declared_locally_in(scope, name, Namespace::Value)
                    {
                        self.emit(
                            "duplicate-declaration",
                            *loc,
                            format!(
                                "`{name}` is already declared in this scope (use `:=` to reassign)"
                            ),
                        );
                    }
                    let type_ref =
                        self.infer_var_type(type_annotation.as_ref(), initializer.as_ref());
                    self.record(
                        Symbol::new(name, SymbolKind::Var, loc.position(), scope)
                            .with_type(type_annotation.clone())
                            .with_type_ref(type_ref),
                    );
                }
            }
            Stmt::Assignment { target, value } => {
                self.check_expr(value);
                self.check_assign_target(target);
            }
            Stmt::TupleAssignment {
                names, value, loc, ..
            } => {
                self.check_expr(value);
                let scope = self.current_scope();
                for name in names {
                    // `_` is a discard, not a binding: it never collides and is
                    // not recorded.
                    if name == "_" {
                        continue;
                    }
                    self.check_shadow(name, *loc);
                    if self
                        .symbols
                        .declared_locally_in(scope, name, Namespace::Value)
                    {
                        self.emit(
                            "duplicate-declaration",
                            *loc,
                            format!("`{name}` is already declared in this scope"),
                        );
                    }
                    self.record(Symbol::new(name, SymbolKind::Var, loc.position(), scope));
                }
            }
            Stmt::Expression(expr) => self.check_expr(expr),
            Stmt::If {
                condition,
                then_branch,
                else_if_branches,
                else_branch,
            } => {
                self.check_expr(condition);
                self.block(then_branch);
                for (cond, body) in else_if_branches {
                    self.check_expr(cond);
                    self.block(body);
                }
                if let Some(body) = else_branch {
                    self.block(body);
                }
            }
            Stmt::For {
                var_name,
                from,
                to,
                step,
                body,
                loc,
            } => {
                self.check_expr(from);
                self.check_expr(to);
                if let Some(step) = step {
                    self.check_expr(step);
                }
                self.enter_scope(ScopeKind::Block);
                self.check_shadow(var_name, *loc);
                let scope = self.current_scope();
                self.record(Symbol::new(
                    var_name,
                    SymbolKind::Var,
                    loc.position(),
                    scope,
                ));
                self.loop_body(body);
                self.exit_scope();
            }
            Stmt::ForIn {
                index_var,
                item_var,
                collection,
                body,
                loc,
            } => {
                self.check_expr(collection);
                self.enter_scope(ScopeKind::Block);
                let scope = self.current_scope();
                if let Some(idx) = index_var {
                    self.check_shadow(idx, *loc);
                    self.record(Symbol::new(idx, SymbolKind::Var, loc.position(), scope));
                }
                self.check_shadow(item_var, *loc);
                self.record(Symbol::new(
                    item_var,
                    SymbolKind::Var,
                    loc.position(),
                    scope,
                ));
                self.loop_body(body);
                self.exit_scope();
            }
            Stmt::While { condition, body } => {
                self.check_expr(condition);
                self.enter_scope(ScopeKind::Block);
                self.loop_body(body);
                self.exit_scope();
            }
            Stmt::Break { loc } => self.check_loop_keyword("break", *loc),
            Stmt::Continue { loc } => self.check_loop_keyword("continue", *loc),
            Stmt::FunctionDecl {
                name,
                params,
                body,
                export,
                loc,
            } => {
                self.check_shadow(name, *loc);
                let id = self.analyze_function(name, *loc, params, body);
                if *export {
                    self.symbols.mark_exported(id);
                }
            }
            Stmt::MethodDecl {
                name,
                params,
                body,
                export,
                loc,
            } => {
                // Methods overload by receiver type, so the name is not duplicate-checked.
                let scope = self.current_scope();
                let id = self.record(
                    Symbol::new(name, SymbolKind::Function, loc.position(), scope)
                        .with_params(params.iter().map(|p| p.name.clone()).collect()),
                );
                if *export {
                    self.symbols.mark_exported(id);
                }
                for param in params {
                    self.check_type_annotation(param.type_annotation.as_ref(), param.loc);
                }
                self.function_body(
                    params.iter().map(|p| {
                        (
                            p.name.as_str(),
                            p.default_value.as_ref(),
                            p.loc,
                            p.type_annotation.as_ref(),
                        )
                    }),
                    body,
                );
            }
            Stmt::TypeDecl {
                name,
                fields,
                export,
                loc,
            } => {
                let owner = self.declare(name, SymbolKind::Type, *loc);
                if *export {
                    self.symbols.mark_exported(owner);
                }
                for field in fields {
                    self.check_type_annotation(Some(&field.type_annotation), field.loc);
                    self.symbols.declare_member(
                        owner,
                        &field.name,
                        field.loc.position(),
                        Some(field.type_annotation.clone()),
                    );
                }
            }
            Stmt::EnumDecl {
                name,
                fields,
                export,
                loc,
            } => {
                let owner = self.declare(name, SymbolKind::Enum, *loc);
                if *export {
                    self.symbols.mark_exported(owner);
                }
                for field in fields {
                    self.symbols
                        .declare_member(owner, &field.name, field.loc.position(), None);
                }
            }
            Stmt::Import { path, alias, loc } => {
                let id = self.declare(alias, SymbolKind::Import, *loc);
                if let Some((_, root)) = self.resolve_import(path, *loc) {
                    self.symbols.set_module(id, root);
                }
            }
            // `export name` re-exports an already-declared item: mark it exported.
            Stmt::Export { item } => {
                let name = match item {
                    ExportItem::Function(name) | ExportItem::Type(name) => name,
                };
                if let Some(id) = self.symbols.resolve_id(self.current_scope(), name) {
                    self.symbols.mark_exported(id);
                }
            }
        }
    }

    fn check_loop_keyword(&mut self, keyword: &str, loc: Loc) {
        if self.loop_depth == 0 {
            self.emit(
                "break-outside-loop",
                loc,
                format!("`{keyword}` is only valid inside a loop"),
            );
        }
    }

    /// Validate the left-hand side of a `:=` reassignment.
    fn check_assign_target(&mut self, target: &Expr) {
        match target {
            Expr::Variable { name, loc } => match self.resolve(name) {
                Some(SymbolKind::Var) => self.record_use(name, *loc),
                Some(other) => self.emit(
                    "invalid-assignment",
                    *loc,
                    format!("cannot assign to `{name}`, it is a {}", other.noun()),
                ),
                None if self.is_builtin(name) => self.emit(
                    "reassign-builtin",
                    *loc,
                    format!("cannot reassign built-in `{name}`"),
                ),
                None => self.emit(
                    "invalid-assignment",
                    *loc,
                    format!(
                        "cannot assign to undeclared variable `{name}` (declare it with `=` first)"
                    ),
                ),
            },
            // `obj.field := …` or `arr[i] := …`: validate the object/index.
            other => self.check_expr(other),
        }
    }

    fn check_expr(&mut self, expr: &Expr) {
        match expr {
            Expr::Variable { name, loc } => {
                if self.resolve(name).is_none() && !self.is_builtin(name) {
                    self.emit(
                        "undeclared-variable",
                        *loc,
                        format!("undeclared variable `{name}`"),
                    );
                } else {
                    self.record_use(name, *loc);
                }
            }
            Expr::Call {
                callee, args, loc, ..
            } => {
                if let Expr::Variable {
                    name: fname,
                    loc: fname_loc,
                } = callee.as_ref()
                {
                    self.record_use(fname, *fname_loc);
                    if is_global_only(fname) && self.current_scope() != SymbolTable::GLOBAL {
                        self.emit(
                            "global-scope-required",
                            *loc,
                            format!("`{fname}` may only be called in the global scope"),
                        );
                    }
                    if SCRIPT_DECLARATIONS.contains(&fname.as_str()) {
                        self.declarations += 1;
                        if fname == "library" {
                            self.library_declared = true;
                        }
                        if self.declarations > 1 {
                            self.emit(
                                "duplicate-declaration",
                                *loc,
                                "a script may only have one indicator/strategy/library declaration",
                            );
                        }
                    }
                    match self.resolve(fname) {
                        Some(SymbolKind::Function) => {
                            // Record the call as an edge out of the enclosing
                            // function; cycles are found once the walk finishes.
                            if let Some(caller) = self.fn_stack.last() {
                                self.call_edges.push((caller.clone(), fname.clone(), *loc));
                            }
                            if let Some(&(required, total)) = self.functions.get(fname) {
                                self.check_call_arity(fname, args.len(), required, total, *loc);
                            }
                        }
                        // A value, type or enum is not callable.
                        Some(kind @ (SymbolKind::Var | SymbolKind::Type | SymbolKind::Enum)) => {
                            self.emit(
                                "not-callable",
                                *loc,
                                format!("`{fname}` is a {}, not a function", kind.noun()),
                            );
                        }
                        // An import alias is called through its members, not directly.
                        Some(SymbolKind::Import) => {}
                        None => {
                            if !self.is_builtin(fname) {
                                self.emit(
                                    "unknown-function",
                                    *loc,
                                    format!("unknown function `{fname}`"),
                                );
                            }
                        }
                    }
                } else {
                    self.check_expr(callee);
                }
                if let Some(signature) = self.builtin_signature(callee) {
                    let name = callee_name(callee);
                    self.check_builtin_args(&name, signature, args, *loc);
                }
                for arg in args {
                    match arg {
                        Argument::Positional(e) => self.check_expr(e),
                        Argument::Named { value, .. } => self.check_expr(value),
                    }
                }
            }
            Expr::Binary { left, right, .. } => {
                self.check_expr(left);
                self.check_expr(right);
            }
            Expr::Unary { expr, .. } => self.check_expr(expr),
            Expr::Index { expr, index, .. } => {
                self.check_expr(expr);
                self.check_expr(index);
            }
            // When the object's type is known, record the member's occurrence.
            Expr::MemberAccess {
                object,
                member,
                member_loc,
            } => {
                self.check_expr(object);
                if let Some(id) = self.resolve_member(object, member) {
                    let file = self.current_file();
                    self.symbols.record_use(file, member_loc.position(), id);
                } else if self.unknown_member(object, member) {
                    if let Expr::Variable { name, .. } = object.as_ref() {
                        self.emit(
                            "unknown-member",
                            *member_loc,
                            format!("`{name}` has no member `{member}`"),
                        );
                    }
                }
            }
            Expr::Ternary {
                condition,
                then_expr,
                else_expr,
            } => {
                self.check_expr(condition);
                self.check_expr(then_expr);
                self.check_expr(else_expr);
            }
            Expr::IfExpr {
                condition,
                then_expr,
                else_if_branches,
                else_expr,
            } => {
                self.check_expr(condition);
                self.check_expr(then_expr);
                for (cond, e) in else_if_branches {
                    self.check_expr(cond);
                    self.check_expr(e);
                }
                if let Some(e) = else_expr {
                    self.check_expr(e);
                }
            }
            Expr::Switch { value, cases } => {
                self.check_expr(value);
                for (pattern, result) in cases {
                    self.check_expr(pattern);
                    self.check_expr(result);
                }
            }
            Expr::Array(elements) => {
                for e in elements {
                    self.check_expr(e);
                }
            }
            // A lambda: its own scope with parameters bound.
            Expr::Function { params, body } => {
                self.function_body(
                    params.iter().map(|p| {
                        (
                            p.name.as_str(),
                            p.default_value.as_ref(),
                            p.loc,
                            p.type_annotation.as_ref(),
                        )
                    }),
                    body,
                );
            }
            Expr::Literal(_) => {}
        }
    }
}