saltwater 0.10.0

A C compiler written in Rust, with a focus on good error messages.
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
use super::*;
use crate::data::ast::{
    self, Declaration, DeclarationSpecifier, Declarator, Expr, ExternalDeclaration, Initializer,
    TypeName,
};
use crate::data::error::Warning;
use crate::data::*;
use std::convert::{TryFrom, TryInto};

#[derive(Debug)]
enum InternalDeclaratorType {
    Id(InternedStr),
    Pointer {
        qualifiers: Vec<DeclarationSpecifier>,
    },
    Array {
        size: Option<Box<Expr>>,
    },
    Function {
        params: Vec<TypeName>,
        varargs: bool,
    },
}

#[derive(Debug)]
struct InternalDeclarator {
    current: InternalDeclaratorType,
    next: Option<Box<InternalDeclarator>>,
}

impl<I: Lexer> Parser<I> {
    /// ```yacc
    /// external_declaration
    /// : function_definition
    /// | declaration
    /// ;
    ///
    /// declaration
    /// : declaration_specifiers ';'
    /// | declaration_specifiers init_declarator_list ';'
    /// ;
    /// ```
    /// <http://www.quut.com/c/ANSI-C-grammar-y.html#external_declaration>
    pub fn external_declaration(&mut self) -> SyntaxResult<Locatable<ExternalDeclaration>> {
        let (specifiers, specifier_locations) = self.specifiers()?;

        // allow `int;`
        if let Some(token) = self.match_next(&Token::Semicolon) {
            let location = token.location.maybe_merge(specifier_locations);
            self.error_handler.warn(Warning::EmptyDeclaration, location);
            let empty_decl = ExternalDeclaration::Declaration(Declaration {
                specifiers,
                declarators: Vec::new(),
            });
            return Ok(Locatable::new(empty_decl, location));
        }

        let declarator = self.init_declarator()?;
        let mut location = declarator.location.maybe_merge(specifier_locations);
        if self.peek_token() == Some(&Token::LeftBrace) {
            use crate::data::ast::{DeclaratorType, FunctionDefinition};

            // int i = 1 {}
            let func = match declarator.data.declarator.decl {
                DeclaratorType::Function(func) => func,
                _ => return Err(location.with(SyntaxError::NotAFunction(declarator.data))),
            };
            // int f() = 1 { }
            if let Some(init) = declarator.data.init {
                return Err(location.with(SyntaxError::FunctionInitializer(init)));
            }

            let body = self.compound_statement()?;
            let location = location.merge(body.location);
            // int () {}
            let err = location.with(SyntaxError::MissingFunctionName);
            let id = declarator.data.declarator.id.ok_or(err)?;
            let def = FunctionDefinition {
                id,
                body: body.data,
                specifiers,
                declarator: func,
            };
            return Ok(Locatable::new(ExternalDeclaration::Function(def), location));
        }
        let mut decls = vec![declarator];
        let has_typedef = specifiers
            .iter()
            .any(|s| *s == DeclarationSpecifier::Unit(crate::data::ast::UnitSpecifier::Typedef));
        while self.match_next(&Token::Semicolon).is_none() {
            self.expect(Token::Comma)?;
            let decl = self.init_declarator()?;
            location = location.merge(decl.location);
            decls.push(decl);
        }
        if has_typedef {
            // `int *;` is caught later
            for id in decls.iter().filter_map(|d| d.data.declarator.id) {
                self.typedefs.insert(id, ());
            }
        }
        let declaration = Declaration {
            specifiers,
            declarators: decls,
        };
        Ok(Locatable::new(
            ExternalDeclaration::Declaration(declaration),
            location,
        ))
    }
    pub fn type_name(&mut self) -> SyntaxResult<Locatable<TypeName>> {
        use crate::ast::DeclaratorType;

        let (specifiers, specifier_locations) = self.specifiers()?;
        let maybe_declarator = self.declarator(true)?;
        let (location, declarator) = match maybe_declarator {
            None => (
                specifier_locations,
                Declarator {
                    decl: DeclaratorType::End,
                    id: None,
                },
            ),
            Some(decl) => (
                Some(decl.location.maybe_merge(specifier_locations)),
                decl.data.parse_declarator(),
            ),
        };
        let location = match location {
            None => {
                assert_eq!(declarator.decl, DeclaratorType::End);
                return Err(self.next_location().with(SyntaxError::ExpectedType));
            }
            Some(l) => l,
        };
        let type_name = TypeName {
            specifiers,
            declarator,
        };
        Ok(Locatable::new(type_name, location))
    }
    fn specifiers(&mut self) -> SyntaxResult<(Vec<DeclarationSpecifier>, Option<Location>)> {
        let mut specifiers = Vec::new();
        let mut all_locs = None;
        let mut seen_typedef = false;
        while let Some(&Token::Keyword(keyword)) = self.peek_token() {
            let location = self.next_token().unwrap().location;
            let spec = match keyword {
                Keyword::Struct => self.struct_specifier(true, location)?,
                Keyword::Union => self.struct_specifier(false, location)?,
                Keyword::Enum => self.enum_specifier(location)?,
                Keyword::UserTypedef(name) => {
                    // absolute hack: allow awful code like `typedef int I; { I I; }`
                    if !seen_typedef {
                        seen_typedef = true;
                        Locatable::new(DeclarationSpecifier::Typedef(name), location)
                    } else {
                        self.unput(Some(Locatable::new(Token::Id(name), location)));
                        break;
                    }
                }
                other if !other.is_decl_specifier() => {
                    let err = SyntaxError::ExpectedDeclSpecifier(keyword);
                    return Err(location.with(err));
                }
                _ => Locatable::new(keyword.try_into().unwrap(), location),
            };
            all_locs = all_locs.map_or(Some(spec.location), |existing: Location| {
                Some(existing.merge(spec.location))
            });
            specifiers.push(spec.data);
        }
        Ok((specifiers, all_locs))
    }
    /// ```yacc
    /// struct_or_union_specifier
    /// : (struct | union) '{' struct_declaration + '}'
    /// | (struct | union) identifier '{' struct_declaration + '}'
    /// | (struct | union) identifier
    /// ;
    /// ```
    /// <http://www.quut.com/c/ANSI-C-grammar-y.html#struct_or_union_specifier>
    fn struct_specifier(
        &mut self,
        is_struct: bool,
        mut start: Location,
    ) -> SyntaxResult<Locatable<DeclarationSpecifier>> {
        use crate::data::ast::StructSpecifier;

        let name = self.match_id().map(|id| {
            start = start.merge(id.location);
            id.data
        });
        let members = if let Some(token) = self.match_next(&Token::LeftBrace) {
            start = start.merge(token.location);
            let mut members = Vec::new();
            loop {
                if let Some(token) = self.match_next(&Token::RightBrace) {
                    start = start.merge(token.location);
                    break;
                }
                if let Some(token) = self.match_next(&Token::Semicolon) {
                    self.error_handler.warn(
                        Warning::ExtraneousSemicolon("struct declaration is not allowed by ISO"),
                        token.location,
                    );
                    continue;
                }
                let decl = self.struct_declaration_list()?;
                start = start.merge(decl.location);
                members.push(decl.data);
            }
            Some(members)
        } else {
            None
        };
        let spec = StructSpecifier { name, members };
        let spec = if is_struct {
            DeclarationSpecifier::Struct(spec)
        } else {
            DeclarationSpecifier::Union(spec)
        };
        Ok(Locatable::new(spec, start))
    }

    /// ```yacc
    /// struct_declaration: (type_specifier | type_qualifier)+ struct_declarator_list ';'
    ///
    /// struct_declarator_list: struct_declarator (',' struct_declarator)* ;
    ///
    /// struct_declarator
    /// : declarator
    /// | ':' constant_expr  // bitfield, not supported
    /// | declarator ':' constant_expr
    /// ;
    /// ```
    /// <http://www.quut.com/c/ANSI-C-grammar-y.html#struct_declaration>
    fn struct_declaration_list(&mut self) -> SyntaxResult<Locatable<ast::StructDeclarationList>> {
        //use data::lex::LocationTrait;
        let (specifiers, mut spec_location) = self.specifiers()?;
        let mut declarators = Vec::new();
        let location = loop {
            if let Some(token) = self.match_next(&Token::Semicolon) {
                break token.location.maybe_merge(spec_location);
            }
            let decl = if self.peek_token() != Some(&Token::Colon) {
                self.declarator(true)?.map(|d| {
                    spec_location = Some(d.location.maybe_merge(spec_location));
                    let mut decl = d.data.parse_declarator();
                    if decl.id.is_none() {
                        let err = Locatable::new(
                            SyntaxError::Generic("struct members must have an id".into()),
                            d.location,
                        );
                        self.error_handler.push_back(err);
                        decl.id = Some("<unnamed member>".into());
                    }
                    decl
                })
            } else {
                None
            };
            let bitfield = if let Some(token) = self.match_next(&Token::Colon) {
                let size = self.ternary_expr()?;
                spec_location = Some(token.location.merge(size.location));
                Some(size)
            } else {
                None
            };
            declarators.push(ast::StructDeclarator { decl, bitfield });
            if self.match_next(&Token::Comma).is_none() {
                break self.expect(Token::Semicolon)?.location;
            }
        };
        let decl_list = ast::StructDeclarationList {
            specifiers,
            declarators,
        };
        Ok(Locatable::new(
            decl_list,
            location.maybe_merge(spec_location),
        ))
    }
    /// ```yacc
    /// enum_specifier
    /// : 'enum' '{' enumerator_list '}'
    /// | 'enum' identifier '{' enumerator_list '}'

    // this is not valid for declaring an enum, but it's fine for an enum we've already seen
    // e.g. `enum E { A }; enum E e;`

    /// | 'enum' identifier
    /// ;
    ///
    /// enumerator_list
    /// : enumerator
    /// | enumerator_list ',' enumerator
    /// ;
    ///
    /// enumerator
    /// : IDENTIFIER
    /// | IDENTIFIER '=' constant_expression
    /// ;
    /// ```
    /// <http://www.quut.com/c/ANSI-C-grammar-y.html#enum_specifier>

    // we've already seen an `enum` token,, `location` is where we saw it
    fn enum_specifier(
        &mut self,
        mut location: Location,
    ) -> SyntaxResult<Locatable<DeclarationSpecifier>> {
        let name = self.match_id().map(|id| {
            location = location.merge(id.location);
            id.data
        });
        let body = if let Some(token) = self.match_next(&Token::LeftBrace) {
            location = location.merge(token.location);
            let mut body = Vec::new();
            loop {
                if let Some(token) = self.match_next(&Token::RightBrace) {
                    location = location.merge(token.location);
                    break;
                }
                let enumerator = self.expect_id()?;
                let value = if self.match_next(&Token::EQUAL).is_some() {
                    Some(self.ternary_expr()?)
                } else {
                    None
                };
                body.push((enumerator.data, value));
                if self.match_next(&Token::Comma).is_none() {
                    let token = self.expect(Token::RightBrace)?;
                    location = location.merge(token.location);
                    break;
                }
            }
            Some(body)
        } else {
            None
        };
        let decl = DeclarationSpecifier::Enum {
            name,
            members: body,
        };
        Ok(Locatable::new(decl, location))
    }

    fn init_declarator(&mut self) -> SyntaxResult<Locatable<ast::InitDeclarator>> {
        let decl = self.declarator(false)?;
        let init = if self.match_next(&Token::EQUAL).is_some() {
            Some(self.initializer()?)
        } else {
            None
        };
        // TODO: this location is wrong
        let location = self.last_location;
        let decl = decl.ok_or_else(|| location.with(SyntaxError::ExpectedDeclarator))?;
        Ok(decl.map(|d| ast::InitDeclarator {
            declarator: InternalDeclarator::parse_declarator(d),
            init,
        }))
    }

    fn merge_decls(
        current: Locatable<InternalDeclaratorType>,
        next: Option<Locatable<InternalDeclarator>>,
    ) -> Locatable<InternalDeclarator> {
        if let Some(next) = next {
            let location = current.location.merge(next.location);
            let decl = InternalDeclarator {
                current: current.data,
                next: Some(Box::new(next.data)),
            };
            Locatable::new(decl, location)
        } else {
            current.map(|data| InternalDeclarator {
                current: data,
                next: None,
            })
        }
    }
    fn declarator(
        &mut self,
        allow_abstract: bool,
    ) -> SyntaxResult<Option<Locatable<InternalDeclarator>>> {
        let mut pointer_decls = Vec::new();
        // NOTE: outdated comment
        // decls coming earlier in the Vec have lower precedence than the ones coming later
        // e.g. `*const *volatile p` would look like `vec![Pointer(const), Pointer(volatile), Id("p")]`
        // and  `*const (*f)()` would look like `vec![Pointer(const), Function, Pointer, Id("f")]`
        // anything to the left of a `Function` represents the return type
        // anything to the right represents a declarator with higher precedence
        // the `Id` should always be the last declarator in the Vec
        while let Some(Locatable { mut location, .. }) = self.match_next(&Token::Star) {
            let mut qualifiers = Vec::new();
            // *const volatile p
            while let Some(Locatable {
                location: keyword_loc,
                data: Token::Keyword(keyword),
            }) = self.match_any(&[
                &Token::Keyword(Keyword::Const),
                &Token::Keyword(Keyword::Volatile),
                &Token::Keyword(Keyword::Restrict),
                &Token::Keyword(Keyword::Atomic),
                &Token::Keyword(Keyword::ThreadLocal),
            ]) {
                location = location.merge(keyword_loc);
                qualifiers.push(keyword.try_into().unwrap());
            }
            let current = Locatable::new(InternalDeclaratorType::Pointer { qualifiers }, location);
            pointer_decls.push(current);
        }
        let mut decl = self.direct_declarator(allow_abstract)?;
        while let Some(pointer) = pointer_decls.pop() {
            decl = Some(Self::merge_decls(pointer, decl));
        }
        Ok(decl)
    }
    /*
     * Originally written as follows:
     * direct_declarator
     *  : identifier
     *  | '(' declarator ')'
     *  | direct_declarator '[' ']'
     *  | direct_declarator '[' constant_expr ']'
     *  | direct_declarator '(' ')'
     *  | direct_declarator '(' parameter_type_list ')'
     *  ;
     * <http://www.quut.com/c/ANSI-C-grammar-y.html#direct_declarator>
     *
     * Additionally, we combine abstract_declarators, because most of the code is the same.
     * direct_abstract_declarator
     *  : '(' abstract_declarator ')'
     *  | '[' ']'
     *  | '[' constant_expr ']'
     *  | direct_abstract_declarator '[' ']'
     *  | direct_abstract_declarator '[' constant_expr ']'
     *  | '(' ')'
     *  | '(' parameter_type_list ')'
     *  | direct_abstract_declarator '(' ')'
     *  | direct_abstract_declarator '(' parameter_type_list ')'
     *  ;
     * <http://www.quut.com/c/ANSI-C-grammar-y.html#direct_abstract_declarator>
     *
     * Because we can't handle left-recursion, we rewrite it as follows:
     * direct_abstract_declarator
     *   : '(' abstract_declarator ')' postfix_type*
     *   | identifier postfix_type*
     *   | postfix_type*  /* only for abstract_declarators */
     *   ;
     *
     * postfix_type:
     *   : '[' ']'
     *   | '[' constant_expr ']'
     *   | '(' ')'
     *   | '(' parameter_type_list ')'
     *   ;
     * ```
     *
     *   How do we tell abstract_declarator and parameter_type_list apart?
     *   parameter_type_list starts with declaration specifiers, abstract_declarator doesn't:
     *   https://stackoverflow.com/questions/56410673/how-should-int-fint-be-parsed
     */
    fn direct_declarator(
        &mut self,
        allow_abstract: bool,
    ) -> SyntaxResult<Option<Locatable<InternalDeclarator>>> {
        let _guard = self.recursion_check();
        // we'll pass this to postfix_type in just a second
        // if None, we didn't find an ID
        // should only happen if allow_abstract is true
        let decl: Option<Locatable<InternalDeclarator>> = match self.peek_token() {
            Some(Token::Id(_)) => Some(self.next_token().unwrap().map(|data| match data {
                Token::Id(id) => InternalDeclarator {
                    current: InternalDeclaratorType::Id(id),
                    next: None,
                },
                _ => panic!("peek() should always return the same thing as next()"),
            })),
            // handled by postfix_type
            Some(Token::LeftBracket) if allow_abstract => None,
            Some(Token::LeftParen) => {
                // this is the reason we need to save next - otherwise we
                // consume LeftParen without postfix_type ever seeing it
                match self.peek_next_token() {
                    // HACK: catch function declarators with implicit int
                    // If we see code like the following: `int f(());`,
                    // this does _not_ mean a parenthesized declarator. Instead,
                    // this is a function declarator with an implicit `int` (compare `int f(int ())`).
                    // This will later be desugared by the analyzer to `int f(int (*)())`.
                    // TODO: this does _not_ catch more complex types like `int f((()))`
                    // (which clang parses as `int f(int (int ()))`) - it instead treats them as `int f(())`.
                    // However, this is so cursed I don't expect it to be a real problem in the real world
                    Some(Token::RightParen) => {
                        let left_paren = self.expect(Token::LeftParen).unwrap().location;
                        let right_paren = self.expect(Token::RightParen).unwrap().location;
                        let decl = InternalDeclarator {
                            current: InternalDeclaratorType::Function {
                                params: Vec::new(),
                                varargs: false,
                            },
                            next: None,
                        };
                        Some(Locatable::new(decl, left_paren.merge(right_paren)))
                    }
                    // parameter_type_list, leave it for postfix_type
                    // need to check allow_abstract because we haven't seen an ID at
                    // this point
                    Some(Token::Keyword(k)) if k.is_decl_specifier() && allow_abstract => None,
                    // abstract_declarator - could be an error,
                    // but if so we'll catch it later
                    _ => {
                        // the one we already matched
                        self.expect(Token::LeftParen)
                            .expect("peek_next_token should be accurate");
                        let declarator = self.declarator(allow_abstract)?;
                        self.expect(Token::RightParen)?;
                        declarator
                    }
                }
            }
            _ if allow_abstract => None,
            Some(x) => {
                let err = Err(Locatable::new(
                    SyntaxError::from(format!("expected variable name or '(', got '{}'", x)),
                    self.next_location(),
                ));
                self.panic();
                return err;
            }
            None => {
                return Err(self.next_location().with(SyntaxError::from(
                    "expected variable name or '(', got <end-of-of-file>",
                )));
            }
        };
        self.postfix_type(decl, allow_abstract)
    }
    /*
     * not in original reference, see comments to `direct_declarator`
     *
     * rewritten grammar:
     *   postfix_type:
     *        '[' ']'
     *      | '[' constant_expr ']'
     *      | '(' ')'
     *      | '(' parameter_type_list ')'
     *      | /* empty */
     *      ;
     */
    #[inline]
    fn postfix_type(
        &mut self,
        mut prefix: Option<Locatable<InternalDeclarator>>,
        allow_abstract: bool,
    ) -> SyntaxResult<Option<Locatable<InternalDeclarator>>> {
        while let Some(data) = self.peek_token() {
            let current = match data {
                // Array; Specified in section 6.7.6.2 of the C11 spec
                Token::LeftBracket => {
                    self.expect(Token::LeftBracket).unwrap();
                    if let Some(token) = self.match_next(&Token::Keyword(Keyword::Static)) {
                        if !allow_abstract {
                            self.error_handler.push_back(Locatable::new(
                                SyntaxError::StaticInConcreteArray,
                                token.location,
                            ));
                        }
                    }
                    let (size, location) =
                        if let Some(token) = self.match_next(&Token::RightBracket) {
                            (None, token.location)
                        } else {
                            let expr = Box::new(self.expr()?);
                            (Some(expr), self.expect(Token::RightBracket)?.location)
                        };
                    Locatable::new(InternalDeclaratorType::Array { size }, location)
                }
                Token::LeftParen => self.parameter_type_list()?,
                _ => break,
            };
            prefix = Some(Self::merge_decls(current, prefix))
        }
        Ok(prefix)
    }
    /*
     * function parameters
     * reference grammar:
     *
     *  parameter_type_list:
     *        parameter_list
     *      | parameter_list ',' ELLIPSIS
     *      ;
     *
     *  parameter_list:
     *        parameter_declaration
     *      | parameter_list ',' parameter_declaration
     *      ;
     *
     *  parameter_declaration:
     *        declaration_specifiers declarator
     *      | declaration_specifiers
     *      | declaration_specifiers abstract_declarator
     *      ;
     *
     * <http://www.quut.com/c/ANSI-C-grammar-y.html#parameter_type_list>
     */
    fn parameter_type_list(&mut self) -> SyntaxResult<Locatable<InternalDeclaratorType>> {
        let left_paren = self
            .expect(Token::LeftParen)
            .expect("parameter_type_list should only be called with '(' as the next token")
            .location;
        let mut params = vec![];
        if let Some(right_paren) = self.match_next(&Token::RightParen) {
            return Ok(Locatable::new(
                InternalDeclaratorType::Function {
                    params,
                    varargs: false,
                },
                left_paren.merge(right_paren.location),
            ));
        }
        loop {
            if self.match_next(&Token::Ellipsis).is_some() {
                let right_paren = self.expect(Token::RightParen)?.location;
                return Ok(Locatable::new(
                    InternalDeclaratorType::Function {
                        params,
                        varargs: true,
                    },
                    left_paren.merge(right_paren),
                ));
            }
            let param = self.type_name()?;
            params.push(param.data);
            if self.match_next(&Token::Comma).is_none() {
                let right_paren = self.expect(Token::RightParen)?.location;
                let location = left_paren.merge(right_paren);
                return Ok(Locatable::new(
                    InternalDeclaratorType::Function {
                        params,
                        varargs: false,
                    },
                    location,
                ));
            }
        }
    }
    fn initializer(&mut self) -> SyntaxResult<Initializer> {
        // initializer_list
        if self.match_next(&Token::LeftBrace).is_some() {
            self.aggregate_initializer()
        } else {
            let expr = self.assignment_expr()?;
            Ok(Initializer::Scalar(Box::new(expr)))
        }
    }

    // handle char[][3] = {{1,2,3}}, but also = {1,2,3} and {{1}, 2, 3}
    // NOTE: this does NOT consume {} except for sub-elements
    fn aggregate_initializer(&mut self) -> SyntaxResult<Initializer> {
        let _guard = self.recursion_check();
        let mut elems = vec![];
        while self.match_next(&Token::RightBrace).is_none() {
            let next = if self.match_next(&Token::LeftBrace).is_some() {
                self.aggregate_initializer()?
            } else {
                // scalar
                self.initializer()?
            };
            elems.push(next);
            // NOTE: this allows trailing commas
            if self.match_next(&Token::Comma).is_none() {
                self.expect(Token::RightBrace)?;
                break;
            };
        }
        Ok(Initializer::Aggregate(elems))
    }
}

impl InternalDeclarator {
    fn parse_declarator(self) -> Declarator {
        use crate::data::ast::DeclaratorType;
        use InternalDeclaratorType::*;

        let mut id = None;
        let mut current = DeclaratorType::End;
        let mut declarator = Some(self);
        while let Some(decl) = declarator {
            current = match decl.current {
                Id(i) => {
                    id = Some(i);
                    current
                }
                Pointer { qualifiers } => DeclaratorType::Pointer {
                    to: Box::new(current),
                    qualifiers,
                },
                Array { size } => DeclaratorType::Array {
                    of: Box::new(current),
                    size,
                },
                Function { params, varargs } => DeclaratorType::from(ast::FunctionDeclarator {
                    return_type: Box::new(current),
                    params,
                    varargs,
                }),
            };
            declarator = decl.next.map(|x| *x);
        }
        Declarator { decl: current, id }
    }
}

impl TryFrom<Keyword> for DeclarationSpecifier {
    type Error = ();
    #[rustfmt::skip]
    fn try_from(k: Keyword) -> Result<DeclarationSpecifier, ()> {
        use ast::UnitSpecifier;

        // TODO: get rid of this macro and store a `enum Keyword { Qualifier(Qualifier), etc. }` instead
        macro_rules! change_enum {
            ($val: expr, $source: path, $dest: ident, $($name: ident),* $(,)?) => {
                match $val {
                    $(<$source>::$name => Ok(DeclarationSpecifier::Unit(UnitSpecifier::$name)),)*
                    _ => Err(()),
                }
            }
        }

        change_enum!(k, Keyword, DeclarationSpecifier,
            Const, Volatile, Restrict, Atomic, ThreadLocal,
            Unsigned, Signed,
            Bool, Char, Short, Int, Long, Float, Double, Void,
            Complex, Imaginary, VaList,
            Extern, Static, Auto, Register, Typedef,
            Inline, NoReturn,
        )
    }
}

impl Token {
    pub(super) fn is_decl_specifier(&self) -> bool {
        match self {
            Token::Keyword(k) => k.is_decl_specifier(),
            //Token::Id(id) => typedefs.get(id).is_some(),
            _ => false,
        }
    }
}

impl Keyword {
    pub(super) fn is_decl_specifier(self) -> bool {
        use Keyword::*;
        match self {
            // type specifier
            Unsigned | Signed | Bool | Char | Short | Int | Long | Float | Double | Void
            // complex type specifier
            | Struct | Union | Enum | VaList | Complex | Imaginary
            // user-defined type
            | UserTypedef(_)
            // storage class
            | Extern | Static | Auto | Register | Typedef
            // qualifier
            | Const | Volatile | Restrict | Atomic | ThreadLocal
            // function qualifier
            | Inline | NoReturn => true,
            _ => false,
        }
    }
}

#[cfg(test)]
pub(crate) mod test {
    use crate::data::ast::*;
    use crate::data::*;
    use crate::parse::test::*;

    fn decl(decl: &str) -> CompileResult<Locatable<ExternalDeclaration>> {
        let mut p = parser(decl);
        let exp = p.external_declaration();
        if let Some(err) = p.error_handler.pop_front() {
            Err(err)
        } else {
            exp.map_err(CompileError::from)
        }
    }
    fn display(s: &str) -> String {
        decl(s).unwrap().data.to_string()
    }

    fn assert_same(left: &str, right: &str) {
        assert_eq!(decl(left), decl(right));
    }
    fn assert_display(left: &str, right: &str) {
        assert_eq!(display(left), right);
    }
    pub(crate) fn assert_no_change(s: &str) {
        assert_display(s, s);
    }

    #[test]
    fn username() {
        assert_no_change("int (*(*jynelson)(int (*)(int)));");
        assert_no_change("const int (*volatile (*restrict jynelson)(_Atomic int (*const volatile )(_Thread_local int)));")
    }
    #[test]
    fn test_precedence() {
        assert_same("char (*(*f));", "char **f;");
    }

    #[test]
    fn test_multiple() {
        assert_no_change("int i, j, k;");
        assert_no_change("int i = 1, j = 2, k = 3;");
        assert_no_change("int i = { { 1 }, 2 };");
    }
    #[test]
    fn test_array() {
        assert_same("int a[10 + 1] = 1;", "int a[(10) + (1)] = 1;");
    }
    #[test]
    fn test_enum() {
        assert!(display("enum { A, B = 2, C };").contains("enum { A, B = 2, C }"));
        assert!(display("enum E { A, B = 2, C };").contains("enum E "));
        // invalid semantically, but valid syntax
        assert!(decl("enum;").is_ok());
    }
    #[test]
    fn test_struct() {
        assert!(decl("struct s { int *; };").is_err());
    }
    #[test]
    fn test_cursed_function_declarator() {
        let decl = parser("f(())")
            .declarator(false)
            .unwrap()
            .unwrap()
            .data
            .parse_declarator();
        assert_eq!(decl.id, Some("f".into()));
        match decl.decl {
            DeclaratorType::Function(FunctionDeclarator {
                return_type,
                params,
                varargs,
            }) => {
                assert_eq!(*return_type, DeclaratorType::End);
                assert_eq!(varargs, false);
                assert_eq!(params.len(), 1);
                let cursed = DeclaratorType::Function(FunctionDeclarator {
                    params: vec![],
                    varargs: false,
                    return_type: Box::new(DeclaratorType::End),
                });
                assert_eq!(params[0].declarator.decl, cursed);
            }
            _ => panic!("wrong declarator parsed"),
        }
    }
}