summaryrefslogtreecommitdiff
path: root/tools/pdl/src/lint.rs
blob: 6c2c389efe46b7a832f536db0e142567f95dff30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
use codespan_reporting::diagnostic::Diagnostic;
use codespan_reporting::files;
use codespan_reporting::term;
use codespan_reporting::term::termcolor;
use std::collections::HashMap;

use crate::ast::*;

/// Aggregate linter diagnostics.
pub struct LintDiagnostics {
    pub diagnostics: Vec<Diagnostic<FileId>>,
}

/// Implement lint checks for an AST element.
pub trait Lintable {
    /// Generate lint warnings and errors for the
    /// input element.
    fn lint(&self) -> LintDiagnostics;
}

/// Represents a chain of group expansion.
/// Each field but the last in the chain is a typedef field of a group.
/// The last field can also be a typedef field of a group if the chain is
/// not fully expanded.
#[derive(Clone)]
struct FieldPath<'d>(Vec<&'d Field>);

/// Gather information about the full grammar declaration.
struct Scope<'d> {
    // Collection of Group, Packet, Enum, Struct, Checksum, and CustomField declarations.
    typedef: HashMap<String, &'d Decl>,

    // Collection of Packet, Struct, and Group scope declarations.
    scopes: HashMap<&'d Decl, PacketScope<'d>>,
}

/// Gather information about a Packet, Struct, or Group declaration.
struct PacketScope<'d> {
    // Checksum starts, indexed by the checksum field id.
    checksums: HashMap<String, FieldPath<'d>>,

    // Size or count fields, indexed by the field id.
    sizes: HashMap<String, FieldPath<'d>>,

    // Payload or body field.
    payload: Option<FieldPath<'d>>,

    // Typedef, scalar, array fields.
    named: HashMap<String, FieldPath<'d>>,

    // Group fields.
    groups: HashMap<String, &'d Field>,

    // Flattened field declarations.
    // Contains field declarations from the original Packet, Struct, or Group,
    // where Group fields have been substituted by their body.
    // Constrained Scalar or Typedef Group fields are substitued by a Fixed
    // field.
    fields: Vec<FieldPath<'d>>,

    // Constraint declarations gathered from Group inlining.
    constraints: HashMap<String, &'d Constraint>,

    // Local and inherited field declarations. Only named fields are preserved.
    // Saved here for reference for parent constraint resolving.
    all_fields: HashMap<String, &'d Field>,

    // Local and inherited constraint declarations.
    // Saved here for constraint conflict checks.
    all_constraints: HashMap<String, &'d Constraint>,
}

impl std::cmp::Eq for &Decl {}
impl<'d> std::cmp::PartialEq for &'d Decl {
    fn eq(&self, other: &Self) -> bool {
        std::ptr::eq(*self, *other)
    }
}

impl<'d> std::hash::Hash for &'d Decl {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        std::ptr::hash(*self, state);
    }
}

impl FieldPath<'_> {
    fn loc(&self) -> &SourceRange {
        self.0.last().unwrap().loc()
    }
}

impl LintDiagnostics {
    fn new() -> LintDiagnostics {
        LintDiagnostics { diagnostics: vec![] }
    }

    pub fn print(
        &self,
        sources: &SourceDatabase,
        color: termcolor::ColorChoice,
    ) -> Result<(), files::Error> {
        let writer = termcolor::StandardStream::stderr(color);
        let config = term::Config::default();
        for d in self.diagnostics.iter() {
            term::emit(&mut writer.lock(), &config, sources, d)?;
        }
        Ok(())
    }

    fn push(&mut self, diagnostic: Diagnostic<FileId>) {
        self.diagnostics.push(diagnostic)
    }

    fn err_undeclared(&mut self, id: &str, loc: &SourceRange) {
        self.diagnostics.push(
            Diagnostic::error()
                .with_message(format!("undeclared identifier `{}`", id))
                .with_labels(vec![loc.primary()]),
        )
    }

    fn err_redeclared(&mut self, id: &str, kind: &str, loc: &SourceRange, prev: &SourceRange) {
        self.diagnostics.push(
            Diagnostic::error()
                .with_message(format!("redeclaration of {} identifier `{}`", kind, id))
                .with_labels(vec![
                    loc.primary(),
                    prev.secondary().with_message(format!("`{}` is first declared here", id)),
                ]),
        )
    }
}

fn bit_width(val: usize) -> usize {
    usize::BITS as usize - val.leading_zeros() as usize
}

impl<'d> PacketScope<'d> {
    /// Insert a field declaration into a packet scope.
    fn insert(&mut self, field: &'d Field, result: &mut LintDiagnostics) {
        match field {
            Field::Checksum { loc, field_id, .. } => {
                self.checksums.insert(field_id.clone(), FieldPath(vec![field])).map(|prev| {
                    result.push(
                        Diagnostic::error()
                            .with_message(format!(
                                "redeclaration of checksum start for `{}`",
                                field_id
                            ))
                            .with_labels(vec![
                                loc.primary(),
                                prev.loc()
                                    .secondary()
                                    .with_message("checksum start is first declared here"),
                            ]),
                    )
                })
            }

            Field::Padding { .. } | Field::Reserved { .. } | Field::Fixed { .. } => None,

            Field::Size { loc, field_id, .. } | Field::Count { loc, field_id, .. } => {
                self.sizes.insert(field_id.clone(), FieldPath(vec![field])).map(|prev| {
                    result.push(
                        Diagnostic::error()
                            .with_message(format!(
                                "redeclaration of size or count for `{}`",
                                field_id
                            ))
                            .with_labels(vec![
                                loc.primary(),
                                prev.loc().secondary().with_message("size is first declared here"),
                            ]),
                    )
                })
            }

            Field::Body { loc, .. } | Field::Payload { loc, .. } => {
                if let Some(prev) = self.payload.as_ref() {
                    result.push(
                        Diagnostic::error()
                            .with_message("redeclaration of payload or body field")
                            .with_labels(vec![
                                loc.primary(),
                                prev.loc()
                                    .secondary()
                                    .with_message("payload is first declared here"),
                            ]),
                    )
                }
                self.payload = Some(FieldPath(vec![field]));
                None
            }

            Field::Array { loc, id, .. }
            | Field::Scalar { loc, id, .. }
            | Field::Typedef { loc, id, .. } => self
                .named
                .insert(id.clone(), FieldPath(vec![field]))
                .map(|prev| result.err_redeclared(id, "field", loc, prev.loc())),

            Field::Group { loc, group_id, .. } => {
                self.groups.insert(group_id.clone(), field).map(|prev| {
                    result.push(
                        Diagnostic::error()
                            .with_message(format!("duplicate group `{}` insertion", group_id))
                            .with_labels(vec![
                                loc.primary(),
                                prev.loc()
                                    .secondary()
                                    .with_message(format!("`{}` is first used here", group_id)),
                            ]),
                    )
                })
            }
        };
    }

    /// Add parent fields and constraints to the scope.
    /// Only named fields are imported.
    fn inherit(
        &mut self,
        scope: &Scope,
        parent: &PacketScope<'d>,
        constraints: impl Iterator<Item = &'d Constraint>,
        result: &mut LintDiagnostics,
    ) {
        // Check constraints.
        assert!(self.all_constraints.is_empty());
        self.all_constraints = parent.all_constraints.clone();
        for constraint in constraints {
            lint_constraint(scope, parent, constraint, result);
            let id = constraint.id.clone();
            if let Some(prev) = self.all_constraints.insert(id, constraint) {
                result.push(
                    Diagnostic::error()
                        .with_message(format!("duplicate constraint on field `{}`", constraint.id))
                        .with_labels(vec![
                            constraint.loc.primary(),
                            prev.loc.secondary().with_message("the constraint is first set here"),
                        ]),
                )
            }
        }

        // Merge group constraints into parent constraints,
        // but generate no duplication warnings, the constraints
        // do no apply to the same field set.
        for (id, constraint) in self.constraints.iter() {
            self.all_constraints.insert(id.clone(), constraint);
        }

        // Save parent fields.
        self.all_fields = parent.all_fields.clone();
    }

    /// Insert group field declarations into a packet scope.
    fn inline(
        &mut self,
        scope: &Scope,
        packet_scope: &PacketScope<'d>,
        group: &'d Field,
        constraints: impl Iterator<Item = &'d Constraint>,
        result: &mut LintDiagnostics,
    ) {
        fn err_redeclared_by_group(
            result: &mut LintDiagnostics,
            message: impl Into<String>,
            loc: &SourceRange,
            prev: &SourceRange,
        ) {
            result.push(Diagnostic::error().with_message(message).with_labels(vec![
                loc.primary(),
                prev.secondary().with_message("first declared here"),
            ]))
        }

        for (id, field) in packet_scope.checksums.iter() {
            if let Some(prev) = self.checksums.insert(id.clone(), field.clone()) {
                err_redeclared_by_group(
                    result,
                    format!("inserted group redeclares checksum start for `{}`", id),
                    group.loc(),
                    prev.loc(),
                )
            }
        }
        for (id, field) in packet_scope.sizes.iter() {
            if let Some(prev) = self.sizes.insert(id.clone(), field.clone()) {
                err_redeclared_by_group(
                    result,
                    format!("inserted group redeclares size or count for `{}`", id),
                    group.loc(),
                    prev.loc(),
                )
            }
        }
        match (&self.payload, &packet_scope.payload) {
            (Some(prev), Some(next)) => err_redeclared_by_group(
                result,
                "inserted group redeclares payload or body field",
                next.loc(),
                prev.loc(),
            ),
            (None, Some(payload)) => self.payload = Some(payload.clone()),
            _ => (),
        }
        for (id, field) in packet_scope.named.iter() {
            let mut path = vec![group];
            path.extend(field.0.clone());
            if let Some(prev) = self.named.insert(id.clone(), FieldPath(path)) {
                err_redeclared_by_group(
                    result,
                    format!("inserted group redeclares field `{}`", id),
                    group.loc(),
                    prev.loc(),
                )
            }
        }

        // Append group fields to the finalizeed fields.
        for field in packet_scope.fields.iter() {
            let mut path = vec![group];
            path.extend(field.0.clone());
            self.fields.push(FieldPath(path));
        }

        // Append group constraints to the caller packet_scope.
        for (id, constraint) in packet_scope.constraints.iter() {
            self.constraints.insert(id.clone(), constraint);
        }

        // Add constraints to the packet_scope, checking for duplicate constraints.
        for constraint in constraints {
            lint_constraint(scope, packet_scope, constraint, result);
            let id = constraint.id.clone();
            if let Some(prev) = self.constraints.insert(id, constraint) {
                result.push(
                    Diagnostic::error()
                        .with_message(format!("duplicate constraint on field `{}`", constraint.id))
                        .with_labels(vec![
                            constraint.loc.primary(),
                            prev.loc.secondary().with_message("the constraint is first set here"),
                        ]),
                )
            }
        }
    }

    /// Cleanup scope after processing all fields.
    fn finalize(&mut self, result: &mut LintDiagnostics) {
        // Check field shadowing.
        for f in self.fields.iter().map(|f| f.0.last().unwrap()) {
            if let Some(id) = f.id() {
                if let Some(prev) = self.all_fields.insert(id.clone(), f) {
                    result.push(
                        Diagnostic::warning()
                            .with_message(format!("declaration of `{}` shadows parent field", id))
                            .with_labels(vec![
                                f.loc().primary(),
                                prev.loc()
                                    .secondary()
                                    .with_message(format!("`{}` is first declared here", id)),
                            ]),
                    )
                }
            }
        }
    }
}

/// Helper for linting value constraints over packet fields.
fn lint_constraint(
    scope: &Scope,
    packet_scope: &PacketScope,
    constraint: &Constraint,
    result: &mut LintDiagnostics,
) {
    // Validate constraint value types.
    match (packet_scope.all_fields.get(&constraint.id), &constraint.value) {
        (
            Some(Field::Scalar { loc: field_loc, width, .. }),
            Expr::Integer { value, loc: value_loc, .. },
        ) => {
            if bit_width(*value) > *width {
                result.push(
                    Diagnostic::error().with_message("invalid integer literal").with_labels(vec![
                        value_loc.primary().with_message(format!(
                            "expected maximum value of `{}`",
                            (1 << *width) - 1
                        )),
                        field_loc.secondary().with_message("the value is used here"),
                    ]),
                )
            }
        }

        (Some(Field::Typedef { type_id, loc: field_loc, .. }), _) => {
            match (scope.typedef.get(type_id), &constraint.value) {
                (Some(Decl::Enum { tags, .. }), Expr::Identifier { name, loc: name_loc, .. }) => {
                    if !tags.iter().any(|t| &t.id == name) {
                        result.push(
                            Diagnostic::error()
                                .with_message(format!("undeclared enum tag `{}`", name))
                                .with_labels(vec![
                                    name_loc.primary(),
                                    field_loc.secondary().with_message("the value is used here"),
                                ]),
                        )
                    }
                }
                (Some(Decl::Enum { .. }), _) => result.push(
                    Diagnostic::error().with_message("invalid literal type").with_labels(vec![
                        constraint
                            .loc
                            .primary()
                            .with_message(format!("expected `{}` tag identifier", type_id)),
                        field_loc.secondary().with_message("the value is used here"),
                    ]),
                ),
                (Some(decl), _) => result.push(
                    Diagnostic::error().with_message("invalid constraint").with_labels(vec![
                        constraint.loc.primary(),
                        field_loc.secondary().with_message(format!(
                            "`{}` has type {}, expected enum field",
                            constraint.id,
                            decl.kind()
                        )),
                    ]),
                ),
                // This error will be reported during field linting
                (None, _) => (),
            }
        }

        (Some(Field::Scalar { loc: field_loc, .. }), _) => {
            result.push(Diagnostic::error().with_message("invalid literal type").with_labels(vec![
                constraint.loc.primary().with_message("expected integer literal"),
                field_loc.secondary().with_message("the value is used here"),
            ]))
        }
        (Some(_), _) => unreachable!(),
        (None, _) => result.push(
            Diagnostic::error()
                .with_message(format!("undeclared identifier `{}`", constraint.id))
                .with_labels(vec![constraint.loc.primary()]),
        ),
    }
}

impl<'d> Scope<'d> {
    // Sort Packet, Struct, and Group declarations by reverse topological
    // orde, and inline Group fields.
    // Raises errors and warnings for:
    //      - undeclared included Groups,
    //      - undeclared Typedef fields,
    //      - undeclared Packet or Struct parents,
    //      - recursive Group insertion,
    //      - recursive Packet or Struct inheritance.
    fn finalize(&mut self, result: &mut LintDiagnostics) -> Vec<&'d Decl> {
        // Auxiliary function implementing BFS on Packet tree.
        enum Mark {
            Temporary,
            Permanent,
        }
        struct Context<'d> {
            list: Vec<&'d Decl>,
            visited: HashMap<&'d Decl, Mark>,
            scopes: HashMap<&'d Decl, PacketScope<'d>>,
        }

        fn bfs<'s, 'd>(
            decl: &'d Decl,
            context: &'s mut Context<'d>,
            scope: &Scope<'d>,
            result: &mut LintDiagnostics,
        ) -> Option<&'s PacketScope<'d>> {
            match context.visited.get(&decl) {
                Some(Mark::Permanent) => return context.scopes.get(&decl),
                Some(Mark::Temporary) => {
                    result.push(
                        Diagnostic::error()
                            .with_message(format!(
                                "recursive declaration of {} `{}`",
                                decl.kind(),
                                decl.id().unwrap()
                            ))
                            .with_labels(vec![decl.loc().primary()]),
                    );
                    return None;
                }
                _ => (),
            }

            let (parent_id, fields) = match decl {
                Decl::Packet { parent_id, fields, .. } | Decl::Struct { parent_id, fields, .. } => {
                    (parent_id.as_ref(), fields)
                }
                Decl::Group { fields, .. } => (None, fields),
                _ => return None,
            };

            context.visited.insert(decl, Mark::Temporary);
            let mut lscope = decl.scope(result).unwrap();

            // Iterate over Struct and Group fields.
            for f in fields {
                match f {
                    Field::Group { group_id, constraints, .. } => {
                        match scope.typedef.get(group_id) {
                            None => result.push(
                                Diagnostic::error()
                                    .with_message(format!(
                                        "undeclared group identifier `{}`",
                                        group_id
                                    ))
                                    .with_labels(vec![f.loc().primary()]),
                            ),
                            Some(group_decl @ Decl::Group { .. }) => {
                                // Recurse to flatten the inserted group.
                                if let Some(rscope) = bfs(group_decl, context, scope, result) {
                                    // Inline the group fields and constraints into
                                    // the current scope.
                                    lscope.inline(scope, rscope, f, constraints.iter(), result)
                                }
                            }
                            Some(_) => result.push(
                                Diagnostic::error()
                                    .with_message(format!(
                                        "invalid group field identifier `{}`",
                                        group_id
                                    ))
                                    .with_labels(vec![f.loc().primary()])
                                    .with_notes(vec!["hint: expected group identifier".to_owned()]),
                            ),
                        }
                    }
                    Field::Typedef { type_id, .. } => {
                        lscope.fields.push(FieldPath(vec![f]));
                        match scope.typedef.get(type_id) {
                            None => result.push(
                                Diagnostic::error()
                                    .with_message(format!(
                                        "undeclared typedef identifier `{}`",
                                        type_id
                                    ))
                                    .with_labels(vec![f.loc().primary()]),
                            ),
                            Some(struct_decl @ Decl::Struct { .. }) => {
                                bfs(struct_decl, context, scope, result);
                            }
                            Some(_) => (),
                        }
                    }
                    _ => lscope.fields.push(FieldPath(vec![f])),
                }
            }

            // Iterate over parent declaration.
            let parent = parent_id.and_then(|id| scope.typedef.get(id));
            match (decl, parent) {
                (Decl::Packet { parent_id: Some(_), .. }, None)
                | (Decl::Struct { parent_id: Some(_), .. }, None) => result.push(
                    Diagnostic::error()
                        .with_message(format!(
                            "undeclared parent identifier `{}`",
                            parent_id.unwrap()
                        ))
                        .with_labels(vec![decl.loc().primary()])
                        .with_notes(vec![format!("hint: expected {} parent", decl.kind())]),
                ),
                (Decl::Packet { .. }, Some(Decl::Struct { .. }))
                | (Decl::Struct { .. }, Some(Decl::Packet { .. })) => result.push(
                    Diagnostic::error()
                        .with_message(format!("invalid parent identifier `{}`", parent_id.unwrap()))
                        .with_labels(vec![decl.loc().primary()])
                        .with_notes(vec![format!("hint: expected {} parent", decl.kind())]),
                ),
                (_, Some(parent_decl)) => {
                    if let Some(rscope) = bfs(parent_decl, context, scope, result) {
                        // Import the parent fields and constraints into the current scope.
                        lscope.inherit(scope, rscope, decl.constraints(), result)
                    }
                }
                _ => (),
            }

            lscope.finalize(result);
            context.list.push(decl);
            context.visited.insert(decl, Mark::Permanent);
            context.scopes.insert(decl, lscope);
            context.scopes.get(&decl)
        }

        let mut context =
            Context::<'d> { list: vec![], visited: HashMap::new(), scopes: HashMap::new() };

        for decl in self.typedef.values() {
            bfs(decl, &mut context, self, result);
        }

        self.scopes = context.scopes;
        context.list
    }
}

impl Field {
    fn kind(&self) -> &str {
        match self {
            Field::Checksum { .. } => "payload",
            Field::Padding { .. } => "padding",
            Field::Size { .. } => "size",
            Field::Count { .. } => "count",
            Field::Body { .. } => "body",
            Field::Payload { .. } => "payload",
            Field::Fixed { .. } => "fixed",
            Field::Reserved { .. } => "reserved",
            Field::Group { .. } => "group",
            Field::Array { .. } => "array",
            Field::Scalar { .. } => "scalar",
            Field::Typedef { .. } => "typedef",
        }
    }
}

// Helper for linting an enum declaration.
fn lint_enum(tags: &[Tag], width: usize, result: &mut LintDiagnostics) {
    let mut local_scope = HashMap::new();
    for tag in tags {
        // Tags must be unique within the scope of the
        // enum declaration.
        if let Some(prev) = local_scope.insert(tag.id.clone(), tag) {
            result.push(
                Diagnostic::error()
                    .with_message(format!("redeclaration of tag identifier `{}`", &tag.id))
                    .with_labels(vec![
                        tag.loc.primary(),
                        prev.loc.secondary().with_message("first declared here"),
                    ]),
            )
        }

        // Tag values must fit the enum declared width.
        if bit_width(tag.value) > width {
            result.push(Diagnostic::error().with_message("invalid literal value").with_labels(
                vec![tag.loc.primary().with_message(format!(
                        "expected maximum value of `{}`",
                        (1 << width) - 1
                    ))],
            ))
        }
    }
}

// Helper for linting checksum fields.
fn lint_checksum(
    scope: &Scope,
    packet_scope: &PacketScope,
    path: &FieldPath,
    field_id: &str,
    result: &mut LintDiagnostics,
) {
    // Checksum field must be declared before
    // the checksum start. The field must be a typedef with
    // a valid checksum type.
    let checksum_loc = path.loc();
    let field_decl = packet_scope.named.get(field_id);

    match field_decl.and_then(|f| f.0.last()) {
        Some(Field::Typedef { loc: field_loc, type_id, .. }) => {
            // Check declaration type of checksum field.
            match scope.typedef.get(type_id) {
                Some(Decl::Checksum { .. }) => (),
                Some(decl) => result.push(
                    Diagnostic::error()
                        .with_message(format!("checksum start uses invalid field `{}`", field_id))
                        .with_labels(vec![
                            checksum_loc.primary(),
                            field_loc.secondary().with_message(format!(
                                "`{}` is declared with {} type `{}`, expected checksum_field",
                                field_id,
                                decl.kind(),
                                type_id
                            )),
                        ]),
                ),
                // This error case will be reported when the field itself
                // is checked.
                None => (),
            };
            // Check declaration order of checksum field.
            match field_decl.and_then(|f| f.0.first()) {
                Some(decl) if decl.loc().start > checksum_loc.start => result.push(
                    Diagnostic::error()
                        .with_message("invalid checksum start declaration")
                        .with_labels(vec![
                            checksum_loc
                                .primary()
                                .with_message("checksum start precedes checksum field"),
                            decl.loc().secondary().with_message("checksum field is declared here"),
                        ]),
                ),
                _ => (),
            }
        }
        Some(field) => result.push(
            Diagnostic::error()
                .with_message(format!("checksum start uses invalid field `{}`", field_id))
                .with_labels(vec![
                    checksum_loc.primary(),
                    field.loc().secondary().with_message(format!(
                        "`{}` is declared as {} field, expected typedef",
                        field_id,
                        field.kind()
                    )),
                ]),
        ),
        None => result.err_undeclared(field_id, checksum_loc),
    }
}

// Helper for linting size fields.
fn lint_size(
    _scope: &Scope,
    packet_scope: &PacketScope,
    path: &FieldPath,
    field_id: &str,
    _width: usize,
    result: &mut LintDiagnostics,
) {
    // Size fields should be declared before
    // the sized field (body, payload, or array).
    // The field must reference a valid body, payload or array
    // field.

    let size_loc = path.loc();

    if field_id == "_payload_" {
        return match packet_scope.payload.as_ref().and_then(|f| f.0.last()) {
            Some(Field::Body { .. }) => result.push(
                Diagnostic::error()
                    .with_message("size field uses undeclared payload field, did you mean _body_ ?")
                    .with_labels(vec![size_loc.primary()]),
            ),
            Some(Field::Payload { .. }) => {
                match packet_scope.payload.as_ref().and_then(|f| f.0.first()) {
                    Some(field) if field.loc().start < size_loc.start => result.push(
                        Diagnostic::error().with_message("invalid size field").with_labels(vec![
                            size_loc
                                .primary()
                                .with_message("size field is declared after payload field"),
                            field.loc().secondary().with_message("payload field is declared here"),
                        ]),
                    ),
                    _ => (),
                }
            }
            Some(_) => unreachable!(),
            None => result.push(
                Diagnostic::error()
                    .with_message("size field uses undeclared payload field")
                    .with_labels(vec![size_loc.primary()]),
            ),
        };
    }
    if field_id == "_body_" {
        return match packet_scope.payload.as_ref().and_then(|f| f.0.last()) {
            Some(Field::Payload { .. }) => result.push(
                Diagnostic::error()
                    .with_message("size field uses undeclared body field, did you mean _payload_ ?")
                    .with_labels(vec![size_loc.primary()]),
            ),
            Some(Field::Body { .. }) => {
                match packet_scope.payload.as_ref().and_then(|f| f.0.first()) {
                    Some(field) if field.loc().start < size_loc.start => result.push(
                        Diagnostic::error().with_message("invalid size field").with_labels(vec![
                            size_loc
                                .primary()
                                .with_message("size field is declared after body field"),
                            field.loc().secondary().with_message("body field is declared here"),
                        ]),
                    ),
                    _ => (),
                }
            }
            Some(_) => unreachable!(),
            None => result.push(
                Diagnostic::error()
                    .with_message("size field uses undeclared body field")
                    .with_labels(vec![size_loc.primary()]),
            ),
        };
    }

    let field = packet_scope.named.get(field_id);

    match field.and_then(|f| f.0.last()) {
        Some(Field::Array { size: Some(_), loc: array_loc, .. }) => result.push(
            Diagnostic::warning()
                .with_message(format!("size field uses array `{}` with static size", field_id))
                .with_labels(vec![
                    size_loc.primary(),
                    array_loc.secondary().with_message(format!("`{}` is declared here", field_id)),
                ]),
        ),
        Some(Field::Array { .. }) => (),
        Some(field) => result.push(
            Diagnostic::error()
                .with_message(format!("invalid `{}` field type", field_id))
                .with_labels(vec![
                    field.loc().primary().with_message(format!(
                        "`{}` is declared as {}",
                        field_id,
                        field.kind()
                    )),
                    size_loc
                        .secondary()
                        .with_message(format!("`{}` is used here as array", field_id)),
                ]),
        ),

        None => result.err_undeclared(field_id, size_loc),
    };
    match field.and_then(|f| f.0.first()) {
        Some(field) if field.loc().start < size_loc.start => {
            result.push(Diagnostic::error().with_message("invalid size field").with_labels(vec![
                    size_loc
                        .primary()
                        .with_message(format!("size field is declared after field `{}`", field_id)),
                    field
                        .loc()
                        .secondary()
                        .with_message(format!("`{}` is declared here", field_id)),
                ]))
        }
        _ => (),
    }
}

// Helper for linting count fields.
fn lint_count(
    _scope: &Scope,
    packet_scope: &PacketScope,
    path: &FieldPath,
    field_id: &str,
    _width: usize,
    result: &mut LintDiagnostics,
) {
    // Count fields should be declared before the sized field.
    // The field must reference a valid array field.
    // Warning if the array already has a known size.

    let count_loc = path.loc();
    let field = packet_scope.named.get(field_id);

    match field.and_then(|f| f.0.last()) {
        Some(Field::Array { size: Some(_), loc: array_loc, .. }) => result.push(
            Diagnostic::warning()
                .with_message(format!("count field uses array `{}` with static size", field_id))
                .with_labels(vec![
                    count_loc.primary(),
                    array_loc.secondary().with_message(format!("`{}` is declared here", field_id)),
                ]),
        ),

        Some(Field::Array { .. }) => (),
        Some(field) => result.push(
            Diagnostic::error()
                .with_message(format!("invalid `{}` field type", field_id))
                .with_labels(vec![
                    field.loc().primary().with_message(format!(
                        "`{}` is declared as {}",
                        field_id,
                        field.kind()
                    )),
                    count_loc
                        .secondary()
                        .with_message(format!("`{}` is used here as array", field_id)),
                ]),
        ),

        None => result.err_undeclared(field_id, count_loc),
    };
    match field.and_then(|f| f.0.first()) {
        Some(field) if field.loc().start < count_loc.start => {
            result.push(Diagnostic::error().with_message("invalid count field").with_labels(vec![
                    count_loc.primary().with_message(format!(
                        "count field is declared after field `{}`",
                        field_id
                    )),
                    field
                        .loc()
                        .secondary()
                        .with_message(format!("`{}` is declared here", field_id)),
                ]))
        }
        _ => (),
    }
}

// Helper for linting fixed fields.
#[allow(clippy::too_many_arguments)]
fn lint_fixed(
    scope: &Scope,
    _packet_scope: &PacketScope,
    path: &FieldPath,
    width: &Option<usize>,
    value: &Option<usize>,
    enum_id: &Option<String>,
    tag_id: &Option<String>,
    result: &mut LintDiagnostics,
) {
    // By parsing constraint, we already have that either
    // (width and value) or (enum_id and tag_id) are Some.

    let fixed_loc = path.loc();

    if width.is_some() {
        // The value of a fixed field should have .
        if bit_width(value.unwrap()) > width.unwrap() {
            result.push(Diagnostic::error().with_message("invalid integer literal").with_labels(
                vec![fixed_loc.primary().with_message(format!(
                    "expected maximum value of `{}`",
                    (1 << width.unwrap()) - 1
                ))],
            ))
        }
    } else {
        // The fixed field should reference a valid enum id and tag id
        // association.
        match scope.typedef.get(enum_id.as_ref().unwrap()) {
            Some(Decl::Enum { tags, .. }) => {
                match tags.iter().find(|t| &t.id == tag_id.as_ref().unwrap()) {
                    Some(_) => (),
                    None => result.push(
                        Diagnostic::error()
                            .with_message(format!(
                                "undeclared enum tag `{}`",
                                tag_id.as_ref().unwrap()
                            ))
                            .with_labels(vec![fixed_loc.primary()]),
                    ),
                }
            }
            Some(decl) => result.push(
                Diagnostic::error()
                    .with_message(format!(
                        "fixed field uses invalid typedef `{}`",
                        decl.id().unwrap()
                    ))
                    .with_labels(vec![fixed_loc.primary().with_message(format!(
                        "{} has kind {}, expected enum",
                        decl.id().unwrap(),
                        decl.kind(),
                    ))]),
            ),
            None => result.push(
                Diagnostic::error()
                    .with_message(format!("undeclared enum type `{}`", enum_id.as_ref().unwrap()))
                    .with_labels(vec![fixed_loc.primary()]),
            ),
        }
    }
}

// Helper for linting array fields.
#[allow(clippy::too_many_arguments)]
fn lint_array(
    scope: &Scope,
    _packet_scope: &PacketScope,
    path: &FieldPath,
    _width: &Option<usize>,
    type_id: &Option<String>,
    _size_modifier: &Option<String>,
    _size: &Option<usize>,
    result: &mut LintDiagnostics,
) {
    // By parsing constraint, we have that width and type_id are mutually
    // exclusive, as well as size_modifier and size.
    // type_id must reference a valid enum or packet type.
    // TODO(hchataing) unbounded arrays should have a matching size
    // or count field

    let array_loc = path.loc();

    if type_id.is_some() {
        match scope.typedef.get(type_id.as_ref().unwrap()) {
            Some(Decl::Enum { .. })
            | Some(Decl::Struct { .. })
            | Some(Decl::CustomField { .. }) => (),
            Some(decl) => result.push(
                Diagnostic::error()
                    .with_message(format!(
                        "array field uses invalid {} element type `{}`",
                        decl.kind(),
                        type_id.as_ref().unwrap()
                    ))
                    .with_labels(vec![array_loc.primary()])
                    .with_notes(vec!["hint: expected enum, struct, custom_field".to_owned()]),
            ),
            None => result.push(
                Diagnostic::error()
                    .with_message(format!(
                        "array field uses undeclared element type `{}`",
                        type_id.as_ref().unwrap()
                    ))
                    .with_labels(vec![array_loc.primary()])
                    .with_notes(vec!["hint: expected enum, struct, custom_field".to_owned()]),
            ),
        }
    }
}

// Helper for linting typedef fields.
fn lint_typedef(
    scope: &Scope,
    _packet_scope: &PacketScope,
    path: &FieldPath,
    type_id: &str,
    result: &mut LintDiagnostics,
) {
    // The typedef field must reference a valid struct, enum,
    // custom_field, or checksum type.
    // TODO(hchataing) checksum fields should have a matching checksum start

    let typedef_loc = path.loc();

    match scope.typedef.get(type_id) {
        Some(Decl::Enum { .. })
        | Some(Decl::Struct { .. })
        | Some(Decl::CustomField { .. })
        | Some(Decl::Checksum { .. }) => (),

        Some(decl) => result.push(
            Diagnostic::error()
                .with_message(format!(
                    "typedef field uses invalid {} element type `{}`",
                    decl.kind(),
                    type_id
                ))
                .with_labels(vec![typedef_loc.primary()])
                .with_notes(vec!["hint: expected enum, struct, custom_field, checksum".to_owned()]),
        ),
        None => result.push(
            Diagnostic::error()
                .with_message(format!("typedef field uses undeclared element type `{}`", type_id))
                .with_labels(vec![typedef_loc.primary()])
                .with_notes(vec!["hint: expected enum, struct, custom_field, checksum".to_owned()]),
        ),
    }
}

// Helper for linting a field declaration.
fn lint_field(
    scope: &Scope,
    packet_scope: &PacketScope,
    field: &FieldPath,
    result: &mut LintDiagnostics,
) {
    match field.0.last().unwrap() {
        Field::Checksum { field_id, .. } => {
            lint_checksum(scope, packet_scope, field, field_id, result)
        }
        Field::Size { field_id, width, .. } => {
            lint_size(scope, packet_scope, field, field_id, *width, result)
        }
        Field::Count { field_id, width, .. } => {
            lint_count(scope, packet_scope, field, field_id, *width, result)
        }
        Field::Fixed { width, value, enum_id, tag_id, .. } => {
            lint_fixed(scope, packet_scope, field, width, value, enum_id, tag_id, result)
        }
        Field::Array { width, type_id, size_modifier, size, .. } => {
            lint_array(scope, packet_scope, field, width, type_id, size_modifier, size, result)
        }
        Field::Typedef { type_id, .. } => lint_typedef(scope, packet_scope, field, type_id, result),
        Field::Padding { .. }
        | Field::Reserved { .. }
        | Field::Scalar { .. }
        | Field::Body { .. }
        | Field::Payload { .. } => (),
        Field::Group { .. } => unreachable!(),
    }
}

// Helper for linting a packet declaration.
fn lint_packet(
    scope: &Scope,
    decl: &Decl,
    id: &str,
    loc: &SourceRange,
    constraints: &[Constraint],
    parent_id: &Option<String>,
    result: &mut LintDiagnostics,
) {
    // The parent declaration is checked by Scope::finalize.
    // The local scope is also generated by Scope::finalize.
    // TODO(hchataing) check parent payload size constraint: compute an upper
    // bound of the payload size and check against the encoded maximum size.

    if parent_id.is_none() && !constraints.is_empty() {
        // Constraint list should be empty when there is
        // no inheritance.
        result.push(
            Diagnostic::warning()
                .with_message(format!(
                    "packet `{}` has field constraints, but no parent declaration",
                    id
                ))
                .with_labels(vec![loc.primary()])
                .with_notes(vec!["hint: expected parent declaration".to_owned()]),
        )
    }

    // Retrieve pre-computed packet scope.
    // Scope validation was done before, so it must exist.
    let packet_scope = &scope.scopes.get(&decl).unwrap();

    for field in packet_scope.fields.iter() {
        lint_field(scope, packet_scope, field, result)
    }
}

// Helper for linting a struct declaration.
fn lint_struct(
    scope: &Scope,
    decl: &Decl,
    id: &str,
    loc: &SourceRange,
    constraints: &[Constraint],
    parent_id: &Option<String>,
    result: &mut LintDiagnostics,
) {
    // The parent declaration is checked by Scope::finalize.
    // The local scope is also generated by Scope::finalize.
    // TODO(hchataing) check parent payload size constraint: compute an upper
    // bound of the payload size and check against the encoded maximum size.

    if parent_id.is_none() && !constraints.is_empty() {
        // Constraint list should be empty when there is
        // no inheritance.
        result.push(
            Diagnostic::warning()
                .with_message(format!(
                    "struct `{}` has field constraints, but no parent declaration",
                    id
                ))
                .with_labels(vec![loc.primary()])
                .with_notes(vec!["hint: expected parent declaration".to_owned()]),
        )
    }

    // Retrieve pre-computed packet scope.
    // Scope validation was done before, so it must exist.
    let packet_scope = &scope.scopes.get(&decl).unwrap();

    for field in packet_scope.fields.iter() {
        lint_field(scope, packet_scope, field, result)
    }
}

impl Decl {
    fn constraints(&self) -> impl Iterator<Item = &Constraint> {
        match self {
            Decl::Packet { constraints, .. } | Decl::Struct { constraints, .. } => {
                Some(constraints.iter())
            }
            _ => None,
        }
        .into_iter()
        .flatten()
    }

    fn scope<'d>(&'d self, result: &mut LintDiagnostics) -> Option<PacketScope<'d>> {
        match self {
            Decl::Packet { fields, .. }
            | Decl::Struct { fields, .. }
            | Decl::Group { fields, .. } => {
                let mut scope = PacketScope {
                    checksums: HashMap::new(),
                    sizes: HashMap::new(),
                    payload: None,
                    named: HashMap::new(),
                    groups: HashMap::new(),

                    fields: Vec::new(),
                    constraints: HashMap::new(),
                    all_fields: HashMap::new(),
                    all_constraints: HashMap::new(),
                };
                for field in fields {
                    scope.insert(field, result)
                }
                Some(scope)
            }
            _ => None,
        }
    }

    fn lint<'d>(&'d self, scope: &Scope<'d>, result: &mut LintDiagnostics) {
        match self {
            Decl::Checksum { .. } | Decl::CustomField { .. } => (),
            Decl::Enum { tags, width, .. } => lint_enum(tags, *width, result),
            Decl::Packet { id, loc, constraints, parent_id, .. } => {
                lint_packet(scope, self, id, loc, constraints, parent_id, result)
            }
            Decl::Struct { id, loc, constraints, parent_id, .. } => {
                lint_struct(scope, self, id, loc, constraints, parent_id, result)
            }
            // Groups are finalizeed before linting, to make sure
            // potential errors are raised only once.
            Decl::Group { .. } => (),
            Decl::Test { .. } => (),
        }
    }

    fn kind(&self) -> &str {
        match self {
            Decl::Checksum { .. } => "checksum",
            Decl::CustomField { .. } => "custom field",
            Decl::Enum { .. } => "enum",
            Decl::Packet { .. } => "packet",
            Decl::Struct { .. } => "struct",
            Decl::Group { .. } => "group",
            Decl::Test { .. } => "test",
        }
    }
}

impl Grammar {
    fn scope<'d>(&'d self, result: &mut LintDiagnostics) -> Scope<'d> {
        let mut scope = Scope { typedef: HashMap::new(), scopes: HashMap::new() };

        // Gather top-level declarations.
        // Validate the top-level scopes (Group, Packet, Typedef).
        //
        // TODO: switch to try_insert when stable
        for decl in &self.declarations {
            if let Some(id) = decl.id() {
                if let Some(prev) = scope.typedef.insert(id.clone(), decl) {
                    result.err_redeclared(id, decl.kind(), decl.loc(), prev.loc())
                }
            }
            if let Some(lscope) = decl.scope(result) {
                scope.scopes.insert(decl, lscope);
            }
        }

        scope.finalize(result);
        scope
    }
}

impl Lintable for Grammar {
    fn lint(&self) -> LintDiagnostics {
        let mut result = LintDiagnostics::new();
        let scope = self.scope(&mut result);
        if !result.diagnostics.is_empty() {
            return result;
        }
        for decl in &self.declarations {
            decl.lint(&scope, &mut result)
        }
        result
    }
}

#[cfg(test)]
mod test {
    use crate::ast::*;
    use crate::lint::Lintable;
    use crate::parser::parse_inline;

    macro_rules! grammar {
        ($db:expr, $text:literal) => {
            parse_inline($db, "stdin".to_owned(), $text.to_owned()).expect("parsing failure")
        };
    }

    #[test]
    fn test_packet_redeclared() {
        let mut db = SourceDatabase::new();
        let grammar = grammar!(
            &mut db,
            r#"
        little_endian_packets
        struct Name { }
        packet Name { }
        "#
        );
        let result = grammar.lint();
        assert!(!result.diagnostics.is_empty());
    }
}