dyon 0.50.3

A rusty dynamically typed scripting 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
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
extern crate piston_meta;
extern crate range;

use self::kind::Kind;
use self::lt::{arg_lifetime, compare_lifetimes, Lifetime};
use self::node::convert_meta_data;
pub(crate) use self::node::Node;
use self::piston_meta::MetaData;
use self::range::Range;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use crate::ast::{AssignOp, UseLookup};
use crate::prelude::{Lt, Prelude};

use crate::Type;

mod kind;
mod lt;
mod node;
mod normalize;
mod typecheck;

/// Checks lifetime constraints and does type checking.
/// Returns refined return types of functions to put in AST.
pub fn check(
    data: &[Range<MetaData>],
    prelude: &Prelude,
) -> Result<HashMap<Arc<String>, Type>, Range<String>> {
    let mut nodes: Vec<Node> = vec![];
    check_core(&mut nodes, data, prelude)
}

// Core lifetime and type check.
pub(crate) fn check_core(
    nodes: &mut Vec<Node>,
    data: &[Range<MetaData>],
    prelude: &Prelude,
) -> Result<HashMap<Arc<String>, Type>, Range<String>> {
    convert_meta_data(nodes, data)?;

    // Rewrite multiple binary operators into nested ones.
    for i in 0..nodes.len() {
        if nodes[i].binops.len() <= 1 {
            continue;
        };

        let new_child = nodes.len();
        let mut parent = nodes[i].parent;
        for n in (2..nodes[i].children.len()).rev() {
            // The right argument of the last call
            // is the last item among the children.
            // The left argument of the last call
            // is the result of the recursion.
            // The last call gets at the top.
            // This means that it gets pushed first.
            // The left argument points to the next node to be pushed,
            // except the last push which points to original node for reuse.
            let id = nodes.len();
            let right = nodes[i].children[n];
            nodes[right].parent = Some(id);
            nodes.push(Node {
                kind: nodes[i].kind,
                names: vec![],
                ty: None,
                declaration: None,
                alias: None,
                mutable: false,
                try_flag: false,
                grab_level: 0,
                source: nodes[i].source,
                start: nodes[i].start,
                end: nodes[i].end,
                lifetime: None,
                op: None,
                binops: vec![nodes[i].binops[n - 1]],
                lts: vec![],
                parent,
                children: vec![if n == 2 { i } else { id + 1 }, right],
            });
            parent = Some(id);
        }

        // Remove all children from original node except the two first.
        nodes[i].children.truncate(2);
        // Remove all binary operators from original node except the first.
        nodes[i].binops.truncate(1);
        if let Some(old_parent) = nodes[i].parent {
            // Find the node among the children of the parent,
            // but do not rely on binary search because it might be unsorted.
            // Unsorted children is due to possible inference from other rewrites.
            for j in 0..nodes[old_parent].children.len() {
                if nodes[old_parent].children[j] == i {
                    nodes[old_parent].children[j] = new_child;
                    break;
                }
            }
        }
        // Change parent of original node.
        nodes[i].parent = parent;
    }

    // Rewrite graph for syntax sugar that corresponds to function calls.
    for i in 0..nodes.len() {
        if nodes[i].children.len() == 1 {
            match nodes[i].kind {
                Kind::Norm => Node::rewrite_unop(i, crate::NORM.clone(), nodes),
                Kind::Not => Node::rewrite_unop(i, crate::NOT.clone(), nodes),
                Kind::Neg => Node::rewrite_unop(i, crate::NEG.clone(), nodes),
                _ => {}
            }
        } else if nodes[i].binops.len() == 1 && nodes[i].children.len() == 2 {
            use crate::ast::BinOp::*;

            Node::rewrite_binop(
                i,
                match nodes[i].binops[0] {
                    Add => crate::ADD.clone(),
                    Sub => crate::SUB.clone(),
                    Mul => crate::MUL.clone(),
                    Div => crate::DIV.clone(),
                    Rem => crate::REM.clone(),
                    Pow => crate::POW.clone(),
                    Dot => crate::DOT.clone(),
                    Cross => crate::CROSS.clone(),
                    AndAlso => crate::AND_ALSO.clone(),
                    OrElse => crate::OR_ELSE.clone(),
                    Less => crate::LESS.clone(),
                    LessOrEqual => crate::LESS_OR_EQUAL.clone(),
                    Greater => crate::GREATER.clone(),
                    GreaterOrEqual => crate::GREATER_OR_EQUAL.clone(),
                    Equal => crate::EQUAL.clone(),
                    NotEqual => crate::NOT_EQUAL.clone(),
                },
                nodes,
            );
        }

        if nodes[i].children.len() == 1 {
            match nodes[i].kind {
                Kind::Add | Kind::Mul => Node::simplify(i, nodes),
                _ => {}
            }
        }
    }

    // After graph rewrite, the graph might be unnormalized.
    normalize::fix(nodes);

    // Add mutability information to function names.
    for i in 0..nodes.len() {
        match nodes[i].kind {
            Kind::Fn | Kind::Call => {}
            Kind::CallClosure => {
                let word = nodes[i].name().cloned();
                if let Some(ref word) = word {
                    // Append named syntax to item.
                    let item = nodes[i].find_child_by_kind(nodes, Kind::Item).unwrap();
                    if nodes[item].children.is_empty() {
                        Arc::make_mut(&mut nodes[item].names[0]).push_str(&format!("__{}", word));
                    }
                    // Ignore when using object property,
                    // because the key is unknown anyway.
                }
            }
            _ => continue,
        };
        let mutable_args = nodes[i].children.iter().any(|&arg| {
            (nodes[arg].kind == Kind::Arg || nodes[arg].kind == Kind::CallArg) && nodes[arg].mutable
        });
        if mutable_args {
            let mut name_plus_args = String::from(&***nodes[i].name().unwrap());
            name_plus_args.push('(');
            let mut first = true;
            for &arg in nodes[i]
                .children
                .iter()
                .filter(|&&n| matches!(nodes[n].kind, Kind::Arg | Kind::CallArg))
            {
                if !first {
                    name_plus_args.push(',');
                }
                name_plus_args.push_str(if nodes[arg].mutable { "mut" } else { "_" });
                first = false;
            }
            name_plus_args.push(')');
            nodes[i].names = vec![Arc::new(name_plus_args)];
        }
    }

    // Collect indices to function nodes.
    let functions: Vec<usize> = nodes
        .iter()
        .enumerate()
        .filter(|&(_, n)| n.kind == Kind::Fn)
        .map(|(i, _)| i)
        .collect();

    // Stores number of functions arguments with same index as `functions`.
    // To look up number of arguments, use `.enumerate()` on the loop.
    let mut function_args: Vec<usize> = Vec::with_capacity(functions.len());

    // Collect indices to call nodes.
    let calls: Vec<usize> = nodes
        .iter()
        .enumerate()
        .filter(|&(_, n)| n.kind == Kind::Call)
        .map(|(i, _)| i)
        .collect();

    // Collect indices to in-nodes.
    #[cfg(all(not(target_family = "wasm"), feature = "threading"))]
    let ins: Vec<usize> = nodes
        .iter()
        .enumerate()
        .filter(|&(_, n)| n.kind == Kind::In)
        .map(|(i, _)| i)
        .collect();

    // Collect indices to returns.
    let returns: Vec<usize> = nodes
        .iter()
        .enumerate()
        .filter(|&(_, n)| n.kind == Kind::Return)
        .map(|(i, _)| i)
        .collect();

    // Collect indices to expressions in mathematical declared functions.
    let math_expr: Vec<usize> = nodes
        .iter()
        .enumerate()
        .filter(|&(_, n)| {
            if n.kind != Kind::Expr {
                return false;
            }
            if let Some(parent) = n.parent {
                if nodes[parent].kind != Kind::Fn && nodes[parent].kind != Kind::Closure {
                    return false;
                }
            }
            true
        })
        .map(|(i, _)| i)
        .collect();

    // Collect indices to expressions at end of blocks.
    let end_of_blocks: Vec<usize> = nodes
        .iter()
        .enumerate()
        .filter(|&(i, n)| {
            if n.kind == Kind::Expr && n.children.len() == 1 {
                let ch = n.children[0];
                if !nodes[ch].has_lifetime() {
                    return false;
                }
            }
            if let Some(parent) = n.parent {
                if !nodes[parent].kind.is_block() {
                    return false;
                }
                if *nodes[parent].children.last().unwrap() != i {
                    return false;
                }
                true
            } else {
                false
            }
        })
        .map(|(i, _)| i)
        .collect();

    // Collect indices to declared locals.
    // Stores assign node, item node.
    let locals: Vec<(usize, usize)> = nodes
        .iter()
        .enumerate()
        .filter(|&(_, n)| {
            n.op == Some(AssignOp::Assign)
                && !n.children.is_empty()
                && !nodes[n.children[0]].children.is_empty()
        })
        .map(|(i, n)| {
            // Left argument.
            let j = n.children[0];
            let node = &nodes[j];
            // Item in left argument.
            let j = node.children[0];
            (i, j)
        })
        // Filter out assignments to objects or arrays to get locals only.
        .filter(|&(_, j)| !nodes[j].item_ids())
        .collect();

    // Collect indices to assignments to object or arrays.
    let assigned_locals: Vec<(usize, usize)> = nodes
        .iter()
        .enumerate()
        .filter(|&(_, n)| {
            n.op == Some(AssignOp::Assign)
                && !n.children.is_empty()
                && !nodes[n.children[0]].children.is_empty()
        })
        .map(|(i, n)| {
            // Left argument.
            let j = n.children[0];
            let node = &nodes[j];
            // Item in left argument.
            let j = node.children[0];
            (i, j)
        })
        // Filter to get assignments to objects or arrays only.
        .filter(|&(_, j)| nodes[j].item_ids())
        .collect();

    // Collect indices to mutated locals.
    // Stores assign node, item node.
    let mutated_locals: Vec<(usize, usize)> = nodes
        .iter()
        .enumerate()
        .filter(|&(_, n)| n.op.is_some() && n.op != Some(AssignOp::Assign))
        .map(|(i, n)| {
            // Left argument.
            let j = n.children[0];
            let node = &nodes[j];
            // Item in left argument.
            let j = node.children[0];
            (i, j)
        })
        .collect();

    // Collect indices to references that are not declared.
    let items: Vec<usize> = nodes
        .iter()
        .enumerate()
        .filter(|&(i, n)| {
            n.kind == Kind::Item && locals.binary_search_by(|&(_, it)| it.cmp(&i)).is_err()
        })
        .map(|(i, _)| i)
        .collect();

    // Collect indices to inferred ranges.
    let inferred: Vec<usize> = nodes
        .iter()
        .enumerate()
        .filter(|&(_, n)| n.kind.is_decl_loop() && n.find_child_by_kind(nodes, Kind::End).is_none())
        .map(|(i, _)| i)
        .collect();

    // Link items to their declaration.
    for &i in &items {
        // When `return` is used as variable one does not need to link.
        if nodes[i].name().map(|n| &**n == "return") == Some(true) {
            continue;
        }

        // Check with all the parents to find the declaration.
        let mut child = i;
        let mut parent = nodes[i].parent.expect("Expected parent");
        let mut it: Option<usize> = None;
        // The grab level to search for declaration.
        let mut grab = 0;

        'search: loop {
            if nodes[parent].kind.is_decl_loop()
                || nodes[parent].kind.is_decl_un_loop()
                || nodes[parent].kind.is_in_loop()
            {
                let my_name = nodes[i].name().unwrap();
                for name in &nodes[parent].names {
                    if name == my_name {
                        it = Some(parent);
                        break 'search;
                    }
                }
            }

            let me = nodes[parent]
                .children
                .binary_search(&child)
                .expect("Expected parent to contain child");
            let children = &nodes[parent].children[..me];
            for &j in children.iter().rev() {
                if nodes[j].children.is_empty() {
                    continue;
                }
                // Assign is inside an expression.
                let j = nodes[j].children[0];
                if nodes[j].op != Some(AssignOp::Assign) {
                    continue;
                }
                let left = nodes[j].children[0];
                let item = nodes[left].children[0];
                if nodes[item].name() == nodes[i].name() {
                    if nodes[item].item_ids() {
                        continue;
                    }
                    if grab > 0 {
                        return Err(nodes[i].source.wrap(format!(
                            "Grabbed `{}` has same name as variable.\n\
                            Perhaps the grab level is set too high?",
                            nodes[i].name().expect("Expected name")
                        )));
                    }
                    it = Some(item);
                    break 'search;
                }
            }
            match nodes[parent].parent {
                Some(new_parent) => {
                    child = parent;
                    parent = new_parent;
                    if nodes[parent].kind == Kind::Grab {
                        grab = nodes[parent].grab_level;
                        if grab == 0 {
                            grab = 1;
                        }
                    }
                    if nodes[parent].kind == Kind::Closure {
                        if grab == 0 {
                            // Search only in closure environment one level up.
                            break 'search;
                        }
                        for &j in &nodes[parent].children {
                            let arg = &nodes[j];
                            match arg.kind {
                                Kind::Arg | Kind::Current => {}
                                _ => continue,
                            };
                            if Some(true) == arg.name().map(|n| **n == **nodes[i].name().unwrap()) {
                                if grab > 0 {
                                    return Err(nodes[i].source.wrap(format!(
                                        "Grabbed `{}` has same name as closure argument",
                                        nodes[i].name().expect("Expected name")
                                    )));
                                }
                                it = Some(j);
                                break 'search;
                            }
                        }
                        grab -= 1;
                    }
                }
                None => break,
            }
        }

        match it {
            Some(it) => nodes[i].declaration = Some(it),
            None => {
                if nodes[parent].kind != Kind::Fn && nodes[parent].kind != Kind::Closure {
                    panic!("Top parent is not a function");
                }
                if nodes[i].name().is_none() {
                    panic!("Item has no name");
                }

                // Search among function arguments.
                let mut found: Option<usize> = None;
                for &j in &nodes[parent].children {
                    let arg = &nodes[j];
                    match arg.kind {
                        Kind::Arg | Kind::Current => {}
                        _ => continue,
                    };
                    if Some(true) == arg.name().map(|n| **n == **nodes[i].name().unwrap()) {
                        found = Some(j);
                        break;
                    }
                }
                match found {
                    Some(j) => {
                        nodes[i].declaration = Some(j);
                    }
                    None => {
                        return Err(nodes[i].source.wrap(format!(
                            "Could not find declaration of `{}`",
                            nodes[i].name().expect("Expected name")
                        )));
                    }
                }
            }
        }
    }

    // Report ranges that can not be inferred.
    for &inf in &inferred {
        for name in &nodes[inf].names {
            let mut found = false;
            'item: for &i in &items {
                if nodes[i].declaration != Some(inf) {
                    continue 'item;
                }
                if nodes[i].name() != Some(name) {
                    continue 'item;
                }
                let mut ch = i;
                while let Some(parent) = nodes[ch].parent {
                    match nodes[parent].kind {
                        Kind::Id => {
                            found = true;
                            break 'item;
                        }
                        Kind::Val | Kind::Expr => {}
                        Kind::Mul | Kind::Add => {
                            if nodes[parent].children.len() > 1 {
                                continue 'item;
                            }
                        }
                        _ => continue 'item,
                    }
                    ch = parent;
                }
            }

            if !found {
                return Err(nodes[inf]
                    .source
                    .wrap("Can not infer range from body, use `list[i]` syntax".to_string()));
            }
        }
    }

    // Check for duplicate function arguments.
    let mut arg_names: HashSet<Arc<String>> = HashSet::new();
    for &f in &functions {
        arg_names.clear();
        let mut n = 0;
        for &i in nodes[f]
            .children
            .iter()
            .filter(|&&i| nodes[i].kind == Kind::Arg)
        {
            let name = nodes[i].name().expect("Expected name");
            if arg_names.contains(name) {
                return Err(nodes[i]
                    .source
                    .wrap(format!("Duplicate argument `{}`", name)));
            } else {
                arg_names.insert(name.clone());
            }
            n += 1;
        }
        function_args.push(n);
    }

    // Check extra type information.
    for (i, &f) in functions.iter().enumerate() {
        if nodes[f].ty == Some(Type::Void) {
            for &ch in &nodes[f].children {
                if nodes[ch].kind == Kind::Ty {
                    return Err(nodes[ch].source.wrap(format!(
                        "`{}` has extra type information but does not return anything",
                        nodes[f].name().expect("Expected name")
                    )));
                }
            }
        } else if let Some(ref ret_type) = nodes[f].ty {
            let n = function_args[i];
            for &ch in &nodes[f].children {
                if nodes[ch].kind == Kind::Ty {
                    let mut count = 0;
                    let mut arg = 0;
                    for &ty_ch in nodes[ch].children.iter() {
                        if nodes[ty_ch].kind == Kind::TyArg {
                            if arg < n {
                                if let Some(ref ty_arg_ty) = nodes[ty_ch].ty {
                                    while nodes[nodes[f].children[arg]].kind != Kind::Arg {
                                        arg += 1;
                                    }
                                    if arg < n {
                                        if let Some(ref arg_ty) = nodes[nodes[f].children[arg]].ty {
                                            if !arg_ty.goes_with(ty_arg_ty) {
                                                return Err(nodes[ty_ch].source.wrap(format!(
                                                    "The type `{}` does not work with `{}`",
                                                    ty_arg_ty.description(),
                                                    arg_ty.description()
                                                )));
                                            }
                                        }
                                    }
                                }
                                arg += 1;
                            }
                            count += 1;
                        } else if nodes[ty_ch].kind == Kind::TyRet {
                            if let Some(ref ty_ret) = nodes[ty_ch].ty {
                                if !ret_type.goes_with(ty_ret) {
                                    return Err(nodes[ty_ch].source.wrap(format!(
                                        "The type `{}` does not work with `{}`",
                                        ty_ret.description(),
                                        ret_type.description()
                                    )));
                                }
                            }
                        }
                    }
                    if count != n {
                        return Err(nodes[ch].source.wrap(format!(
                            "Expected {} number of arguments, found {}",
                            n, count
                        )));
                    }
                }
            }
        }
    }

    // Check for duplicate functions and build name to index map.
    let mut function_lookup: HashMap<Arc<String>, usize> = HashMap::new();
    for (i, &f) in functions.iter().enumerate() {
        let name = nodes[f].name().expect("Expected name");
        if function_lookup.contains_key(name) {
            return Err(nodes[f]
                .source
                .wrap(format!("Duplicate function `{}`", name)));
        } else {
            function_lookup.insert(name.clone(), i);
        }
    }

    let mut use_lookup: UseLookup = UseLookup::new();
    for node in nodes.iter() {
        if node.kind == Kind::Uses {
            use crate::ast::Uses;
            use piston_meta::bootstrap::Convert;

            let convert = Convert::new(&data[node.start..node.end]);
            if let Ok((_, val)) = Uses::from_meta_data(convert, &mut vec![]) {
                use_lookup = UseLookup::from_uses_prelude(&val, prelude);
            }
            break;
        }
    }

    // Link call nodes to functions.
    for &c in &calls {
        let n = {
            let mut sum = 0;
            for &ch in nodes[c]
                .children
                .iter()
                .filter(|&&i| nodes[i].kind == Kind::CallArg)
            {
                if let Some(sw) = nodes[ch].find_child_by_kind(nodes, Kind::Swizzle) {
                    sum += nodes[sw]
                        .children
                        .iter()
                        .filter(|&&i| {
                            matches!(nodes[i].kind, Kind::Sw0 | Kind::Sw1 | Kind::Sw2 | Kind::Sw3)
                        })
                        .count();
                } else {
                    sum += 1;
                }
            }
            sum
        };

        let node = &mut nodes[c];
        let name = node.name().expect("Expected name").clone();
        if let Some(ref alias) = node.alias {
            use crate::ast::FnAlias;

            // External functions are treated as loaded in prelude.
            if let Some(&FnAlias::Loaded(i)) =
                use_lookup.aliases.get(alias).and_then(|map| map.get(&name))
            {
                node.lts = prelude.list[i].lts.clone();
                continue;
            } else {
                return Err(node
                    .source
                    .wrap(format!("Could not find function `{}::{}`", alias, name)));
            }
        }
        let i = match function_lookup.get(&name) {
            Some(&i) => i,
            None => {
                // Check whether it is a prelude function.
                if let Some(&pf) = prelude.functions.get(&name) {
                    node.lts = prelude.list[pf].lts.clone();
                    if node.lts.len() != n {
                        return Err(node.source.wrap(format!(
                            "{}: Expected {} arguments, found {}",
                            name,
                            node.lts.len(),
                            n
                        )));
                    }
                    continue;
                }
                let suggestions = suggestions(&**name, &function_lookup, prelude);
                return Err(node
                    .source
                    .wrap(format!("Could not find function `{}`{}", name, suggestions)));
            }
        };
        // Check that number of arguments is the same as in declaration.
        if function_args[i] != n {
            let suggestions = suggestions(&**name, &function_lookup, prelude);
            return Err(node.source.wrap(format!(
                "{}: Expected {} arguments, found {}{}",
                name, function_args[i], n, suggestions
            )));
        }
        node.declaration = Some(functions[i]);
    }

    // Check in-nodes.
    #[cfg(all(not(target_family = "wasm"), feature = "threading"))]
    for &c in &ins {
        let node = &mut nodes[c];
        let name = node.name().expect("Expected name").clone();
        if let Some(ref alias) = node.alias {
            use crate::ast::FnAlias;

            // External functions are treated as loaded in prelude.
            if let Some(&FnAlias::Loaded(i)) =
                use_lookup.aliases.get(alias).and_then(|map| map.get(&name))
            {
                node.lts = prelude.list[i].lts.clone();
                continue;
            } else {
                return Err(node
                    .source
                    .wrap(format!("Could not find function `{}::{}`", alias, name)));
            }
        }
        match function_lookup.get(&name) {
            Some(&i) => i,
            None => {
                // Check whether it is a prelude function.
                if prelude.functions.get(&name).is_some() {
                    continue;
                };
                let suggestions = suggestions(&**name, &function_lookup, prelude);
                return Err(node
                    .source
                    .wrap(format!("Could not find function `{}`{}", name, suggestions)));
            }
        };
    }

    // Build a map from (function, argument_name) => (argument, index).
    let mut arg_names: ArgNames = HashMap::new();
    for (i, &f) in functions.iter().enumerate() {
        let function = &nodes[f];
        for (j, &c) in function
            .children
            .iter()
            .filter(|&&c| nodes[c].kind == Kind::Arg)
            .enumerate()
        {
            let name = nodes[c].name().expect("Expected name");
            arg_names.insert((f, name.clone()), (c, j));
        }
        // Check that all lifetimes except `'return` points to another argument.
        for &c in function
            .children
            .iter()
            .filter(|&&c| nodes[c].kind == Kind::Arg)
        {
            if let Some(ref lt) = nodes[c].lifetime {
                if &**lt == "return" {
                    continue;
                }
                if !arg_names.contains_key(&(f, lt.clone())) {
                    return Err(nodes[c]
                        .source
                        .wrap(format!("Could not find argument `{}`", lt)));
                }
            }
        }

        // Check for cyclic references among lifetimes.
        let mut visited = vec![false; function_args[i]];
        for (_, &c) in function
            .children
            .iter()
            .filter(|&&c| nodes[c].kind == Kind::Arg)
            .enumerate()
        {
            if let Some(ref lt) = nodes[c].lifetime {
                if &**lt == "return" {
                    break;
                }
                // Reset visit flags.
                for it in &mut visited {
                    *it = false;
                }

                let (mut arg, mut ind) = *arg_names
                    .get(&(f, lt.clone()))
                    .expect("Expected argument index");
                loop {
                    if visited[ind] {
                        return Err(nodes[arg]
                            .source
                            .wrap(format!("Cyclic lifetime for `{}`", lt)));
                    }
                    visited[ind] = true;

                    // Go to next argument by following the lifetime.
                    let name = match nodes[arg].lifetime {
                        None => break,
                        Some(ref name) => name.clone(),
                    };
                    if &**name == "return" {
                        break;
                    }
                    let (new_arg, new_ind) = *arg_names.get(&(f, name)).expect("Expected argument");
                    arg = new_arg;
                    ind = new_ind;
                }
            }
        }
    }

    // Check the lifetime of mutated locals.
    for &(a, i) in &mutated_locals {
        // Only `=` needs a lifetime check.
        if nodes[a].op != Some(AssignOp::Set) {
            continue;
        }
        let right = nodes[a].children[1];
        let lifetime_left = &nodes[i].lifetime(nodes, &arg_names);
        let lifetime_right = &nodes[right].lifetime(nodes, &arg_names);
        compare_lifetimes(lifetime_left, lifetime_right, nodes)
            .map_err(|err| nodes[right].source.wrap(err))?;
    }

    // Check the lifetime of declared locals.
    for &(a, i) in &locals {
        let right = nodes[a].children[1];
        let lifetime_left = &Ok(Lifetime::Local(i));
        let lifetime_right = &nodes[right].lifetime(nodes, &arg_names);
        compare_lifetimes(lifetime_left, lifetime_right, nodes)
            .map_err(|err| nodes[right].source.wrap(err))?;
    }

    // Check the lifetime of assigned locals.
    for &(a, i) in &assigned_locals {
        if let Some(j) = nodes[i].declaration {
            let right = nodes[a].children[1];
            let lifetime_left = &Ok(Lifetime::Local(j));
            let lifetime_right = &nodes[right].lifetime(nodes, &arg_names);
            compare_lifetimes(lifetime_left, lifetime_right, nodes)
                .map_err(|err| nodes[right].source.wrap(err))?;
        }
    }

    // Check the lifetime of returned values.
    for &i in &returns {
        let right = nodes[i].children[0];
        let lifetime_right = &nodes[right].lifetime(nodes, &arg_names);
        compare_lifetimes(&Ok(Lifetime::Return(vec![])), lifetime_right, nodes)
            .map_err(|err| nodes[right].source.wrap(err))?;
    }

    // Check the lifetime of expressions that are mathematically declared.
    for &i in &math_expr {
        let lifetime_right = &nodes[i].lifetime(nodes, &arg_names);
        compare_lifetimes(&Ok(Lifetime::Return(vec![])), lifetime_right, nodes)
            .map_err(|err| nodes[i].source.wrap(err))?;
    }

    // Check that no function argument has lifetime "'return"
    // while the function does not return anything.
    for &f in &functions {
        if let Some(Type::Void) = nodes[f].ty {
            for &j in nodes[f]
                .children
                .iter()
                .filter(|&&i| nodes[i].kind == Kind::Arg)
            {
                if let Some(ref lt) = nodes[j].lifetime {
                    if &**lt == "return" {
                        let name = nodes[j].name().expect("Expected name");
                        return Err(nodes[j].source.wrap(format!(
                            "`{}: 'return` , but function does not return",
                            name
                        )));
                    }
                }
            }
        }
    }

    // Check the lifetime of expressions at end of blocks.
    for &i in &end_of_blocks {
        let parent = nodes[i].parent.unwrap();
        // Fake a local variable.
        let lifetime_left = &Ok(Lifetime::Local(parent));
        let lifetime_right = &nodes[i].lifetime(nodes, &arg_names);
        compare_lifetimes(lifetime_left, lifetime_right, nodes)
            .map_err(|err| nodes[i].source.wrap(err))?;
    }

    // Check that calls do not have arguments with shorter lifetime than the call.
    for &c in &calls {
        let call = &nodes[c];
        // Fake a local variable.
        let lifetime_left = &Ok(Lifetime::Local(c));
        for &a in call
            .children
            .iter()
            .filter(|&&i| nodes[i].kind == Kind::CallArg)
        {
            let lifetime_right = &nodes[a].lifetime(nodes, &arg_names);
            compare_lifetimes(lifetime_left, lifetime_right, nodes)
                .map_err(|err| nodes[a].source.wrap(err))?;
        }
    }

    // Check that `go` functions does not have lifetime constraints.
    for &c in &calls {
        let call = &nodes[c];
        #[cfg(all(not(target_family = "wasm"), feature = "threading"))]
        if let Some(parent) = call.parent {
            if nodes[parent].kind != Kind::Go {
                continue;
            }
        } else {
            continue;
        }
        #[cfg(any(target_family = "wasm", not(feature = "threading")))]
        if call.parent.is_none() {
            continue;
        }
        if let Some(declaration) = call.declaration {
            let function = &nodes[declaration];
            for (i, &a) in function
                .children
                .iter()
                .enumerate()
                .filter(|&(_, &i)| nodes[i].kind == Kind::Arg)
            {
                let arg = &nodes[a];
                if arg.lifetime.is_some() {
                    return Err(nodes[call.children[i]].source.wrap(
                        "Can not use `go` because this argument has a lifetime constraint"
                            .to_string(),
                    ));
                }
            }
        } else {
            // Check that call to intrinsic satisfy the declared constraints.
            for ((i, &lt), _) in call.lts.iter().enumerate().zip(
                call.children
                    .iter()
                    .filter(|&&n| nodes[n].kind == Kind::CallArg),
            ) {
                match lt {
                    Lt::Default => {}
                    _ => {
                        return Err(nodes[call.children[i]].source.wrap(
                            "Can not use `go` because this argument has a lifetime constraint"
                                .to_string(),
                        ));
                    }
                }
            }
        }
    }

    // Check that calls satisfy the lifetime constraints of arguments.
    for &c in &calls {
        let call = &nodes[c];
        let map_arg_call_arg_index = |i: usize| {
            // Map `i` to the call arg taking swizzling into account.
            let mut j = 0;
            let mut new_i = 0;
            for &ch in &call.children {
                if let Some(sw) = nodes[ch].find_child_by_kind(nodes, Kind::Swizzle) {
                    for &ch in &nodes[sw].children {
                        j += match nodes[ch].kind {
                            Kind::Sw0 | Kind::Sw1 | Kind::Sw2 | Kind::Sw3 => 1,
                            _ => 0,
                        }
                    }
                } else {
                    j += 1;
                }
                if j > i {
                    break;
                }
                new_i += 1;
            }
            new_i
        };
        let is_reference = |i: usize| {
            let mut n: usize = call.children[i];
            let mut can_be_item = true;
            // Item is some levels down inside arg/add/expr/mul/val
            loop {
                let node: &Node = &nodes[n];
                match node.kind {
                    Kind::Item => break,
                    Kind::Call => {
                        can_be_item = false;
                        break;
                    }
                    _ => {}
                }
                if node.children.is_empty() {
                    can_be_item = false;
                    break;
                }
                n = node.children[0];
            }
            if can_be_item && nodes[n].kind != Kind::Item {
                can_be_item = false;
            }
            can_be_item
        };

        if let Some(declaration) = call.declaration {
            let function = &nodes[declaration];
            for (i, &a) in function
                .children
                .iter()
                .enumerate()
                .filter(|&(_, &i)| nodes[i].kind == Kind::Arg)
                .map(|(i, a)| (map_arg_call_arg_index(i), a))
            {
                let arg = &nodes[a];
                if let Some(ref lt) = arg.lifetime {
                    // When arguments should outlive the return value,
                    // make sure they are referenced.
                    let arg_lifetime = arg_lifetime(a, arg, nodes, &arg_names);
                    match arg_lifetime {
                        Ok(Lifetime::Return(_)) | Ok(Lifetime::Argument(_)) => {
                            if !is_reference(i) {
                                return Err(nodes[call.children[i]]
                                    .source
                                    .wrap("Requires reference to variable".to_string()));
                            }
                        }
                        _ => {}
                    }

                    if &**lt != "return" {
                        // Compare the lifetime of the two arguments.
                        let (_, ind) = *arg_names
                            .get(&(declaration, lt.clone()))
                            .expect("Expected argument name");
                        let left = call.children[ind];
                        let right = call.children[i];
                        let lifetime_left = &nodes[left].lifetime(nodes, &arg_names);
                        let lifetime_right = &nodes[right].lifetime(nodes, &arg_names);
                        compare_lifetimes(lifetime_left, lifetime_right, nodes)
                            .map_err(|err| nodes[right].source.wrap(err))?;
                    }
                }
            }
        } else {
            // Check that call to intrinsic satisfy the declared constraints.
            for (i, &lt) in call
                .lts
                .iter()
                .enumerate()
                .map(|(i, a)| (map_arg_call_arg_index(i), a))
            {
                let arg = &nodes[call.children[i]];
                match lt {
                    Lt::Default => {}
                    Lt::Return => {
                        if !is_reference(i) {
                            return Err(arg
                                .source
                                .wrap("Requires reference to variable".to_string()));
                        }
                    }
                    Lt::Arg(ind) => {
                        if !is_reference(i) {
                            return Err(arg
                                .source
                                .wrap("Requires reference to variable".to_string()));
                        }

                        let left = call.children[ind];
                        let right = call.children[i];
                        let lifetime_left = &nodes[left].lifetime(nodes, &arg_names);
                        let lifetime_right = &nodes[right].lifetime(nodes, &arg_names);
                        compare_lifetimes(lifetime_left, lifetime_right, nodes)
                            .map_err(|err| nodes[right].source.wrap(err))?;
                    }
                }
            }
        }
    }

    // Check that mutable locals are not immutable arguments.
    for &(_, i) in &mutated_locals {
        if let Some(decl) = nodes[i].declaration {
            if (nodes[decl].kind == Kind::Arg || nodes[decl].kind == Kind::Current)
                && !nodes[decl].mutable
            {
                return Err(nodes[i]
                    .source
                    .wrap(format!("Requires `mut {}`", nodes[i].name().unwrap())));
            }
        }
    }

    // Check that calling mutable argument are not immutable.
    for &c in &calls {
        let call = &nodes[c];
        let reference = |i: usize| {
            let mut n: usize = i;
            // Item is 2 levels down inside call_arg/item
            for _ in 0..2 {
                let node: &Node = &nodes[n];
                if node.kind == Kind::Item {
                    return Some(n);
                }
                if node.children.is_empty() {
                    break;
                }
                n = node.children[0];
            }
            None
        };

        for &arg in call
            .children
            .iter()
            .filter(|&&n| nodes[n].kind == Kind::CallArg && nodes[n].mutable)
        {
            if let Some(n) = reference(arg) {
                if let Some(decl) = nodes[n].declaration {
                    if (nodes[decl].kind == Kind::Arg || nodes[decl].kind == Kind::Current)
                        && !nodes[decl].mutable
                    {
                        return Err(nodes[n]
                            .source
                            .wrap(format!("Requires `mut {}`", nodes[n].name().unwrap())));
                    }
                }
            }
        }
    }

    typecheck::run(nodes, prelude, &use_lookup)?;

    // Copy refined return types to use in AST.
    let mut refined_rets: HashMap<Arc<String>, Type> = HashMap::new();
    for (name, &ind) in &function_lookup {
        if let Some(ref ty) = nodes[functions[ind]].ty {
            refined_rets.insert(name.clone(), ty.clone());
        }
    }

    Ok(refined_rets)
}

// Search for suggestions using matching function signature.
// Meant to be put last in error message.
fn suggestions(
    name: &str,
    function_lookup: &HashMap<Arc<String>, usize>,
    prelude: &Prelude,
) -> String {
    let search_name = if let Some((mut_pos, _)) = name.chars().enumerate().find(|&(_, c)| c == '(')
    {
        &name[..mut_pos - 1]
    } else {
        name
    };
    let mut found_suggestions = false;
    let mut suggestions = String::from("\n\nDid you mean:\n");
    for f in function_lookup.keys() {
        if (&***f).starts_with(search_name) {
            suggestions.push_str("- ");
            suggestions.push_str(f);
            suggestions.push('\n');
            found_suggestions = true;
        }
    }
    for f in prelude.functions.keys() {
        if (&***f).starts_with(search_name) {
            suggestions.push_str("- ");
            suggestions.push_str(f);
            suggestions.push('\n');
            found_suggestions = true;
        }
    }
    if found_suggestions {
        suggestions
    } else {
        String::from("")
    }
}

/// Maps (function, argument_name) => (argument, index)
pub type ArgNames = HashMap<(usize, Arc<String>), (usize, usize)>;