Mon Jul 12 21:43:44 2021 UTC ()
lint: reorder grammar rules in the same way as in C99

The code coverage before and after this change is exactly the same,
except of course for cgram.y and cgram.c.

No functional change.


(rillig)
diff -r1.318 -r1.319 src/usr.bin/xlint/lint1/cgram.y

cvs diff -r1.318 -r1.319 src/usr.bin/xlint/lint1/cgram.y (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/cgram.y 2021/07/11 21:07:44 1.318
+++ src/usr.bin/xlint/lint1/cgram.y 2021/07/12 21:43:44 1.319
@@ -1,15 +1,15 @@ @@ -1,15 +1,15 @@
1%{ 1%{
2/* $NetBSD: cgram.y,v 1.318 2021/07/11 21:07:44 rillig Exp $ */ 2/* $NetBSD: cgram.y,v 1.319 2021/07/12 21:43:44 rillig Exp $ */
3 3
4/* 4/*
5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
6 * Copyright (c) 1994, 1995 Jochen Pohl 6 * Copyright (c) 1994, 1995 Jochen Pohl
7 * All Rights Reserved. 7 * All Rights Reserved.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the 15 * notice, this list of conditions and the following disclaimer in the
@@ -25,27 +25,27 @@ @@ -25,27 +25,27 @@
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */ 34 */
35 35
36#include <sys/cdefs.h> 36#include <sys/cdefs.h>
37#if defined(__RCSID) && !defined(lint) 37#if defined(__RCSID) && !defined(lint)
38__RCSID("$NetBSD: cgram.y,v 1.318 2021/07/11 21:07:44 rillig Exp $"); 38__RCSID("$NetBSD: cgram.y,v 1.319 2021/07/12 21:43:44 rillig Exp $");
39#endif 39#endif
40 40
41#include <limits.h> 41#include <limits.h>
42#include <stdlib.h> 42#include <stdlib.h>
43#include <string.h> 43#include <string.h>
44 44
45#include "lint1.h" 45#include "lint1.h"
46 46
47extern char *yytext; 47extern char *yytext;
48 48
49/* 49/*
50 * Contains the level of current declaration, used for symbol table entries. 50 * Contains the level of current declaration, used for symbol table entries.
51 * 0 is the top-level, > 0 is inside a function body. 51 * 0 is the top-level, > 0 is inside a function body.
@@ -353,291 +353,536 @@ anonymize(sym_t *s) @@ -353,291 +353,536 @@ anonymize(sym_t *s)
353program: 353program:
354 /* empty */ { 354 /* empty */ {
355 if (sflag) { 355 if (sflag) {
356 /* empty translation unit */ 356 /* empty translation unit */
357 error(272); 357 error(272);
358 } else if (!tflag) { 358 } else if (!tflag) {
359 /* empty translation unit */ 359 /* empty translation unit */
360 warning(272); 360 warning(272);
361 } 361 }
362 } 362 }
363 | translation_unit 363 | translation_unit
364 ; 364 ;
365 365
366translation_unit: /* C99 6.9 */ 366identifier_sym: /* helper for struct/union/enum */
367 external_declaration 367 identifier {
368 | translation_unit external_declaration 368 $$ = getsym($1);
 369 }
369 ; 370 ;
370 371
371external_declaration: /* C99 6.9 */ 372/* K&R ???, C90 ???, C99 6.4.2.1, C11 ???, C18 ??? */
372 asm_statement 373identifier:
373 | function_definition { 374 T_NAME {
374 global_clean_up_decl(false); 375 $$ = $1;
375 clear_warning_flags(); 376 cgram_debug("name '%s'", $$->sb_name);
376 } 377 }
377 | top_level_declaration { 378 | T_TYPENAME {
378 global_clean_up_decl(false); 379 $$ = $1;
379 clear_warning_flags(); 380 cgram_debug("typename '%s'", $$->sb_name);
 381 }
 382 ;
 383
 384/* see C99 6.4.5, string literals are joined by 5.1.1.2 */
 385string:
 386 T_STRING
 387 | T_STRING string2 {
 388 $$ = cat_strings($1, $2);
 389 }
 390 ;
 391
 392/* see C99 6.4.5, string literals are joined by 5.1.1.2 */
 393string2:
 394 T_STRING {
 395 if (tflag) {
 396 /* concatenated strings are illegal in traditional C */
 397 warning(219);
 398 }
 399 $$ = $1;
 400 }
 401 | string2 T_STRING {
 402 $$ = cat_strings($1, $2);
 403 }
 404 ;
 405
 406/* K&R 7.1, C90 ???, C99 6.5.1, C11 6.5.1, C18 6.5.1 */
 407primary_expression:
 408 T_NAME {
 409 /* XXX really necessary? */
 410 if (yychar < 0)
 411 yychar = yylex();
 412 $$ = new_name_node(getsym($1), yychar);
 413 }
 414 | T_CON {
 415 $$ = expr_new_constant(gettyp($1->v_tspec), $1);
 416 }
 417 | string {
 418 $$ = new_string_node($1);
 419 }
 420 | T_LPAREN expr T_RPAREN {
 421 if ($2 != NULL)
 422 $2->tn_parenthesized = true;
 423 $$ = $2;
 424 }
 425 | generic_selection
 426 /* GCC primary-expression, see c_parser_postfix_expression */
 427 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA identifier T_RPAREN {
 428 symtyp = FMEMBER;
 429 $$ = build_offsetof($3, getsym($5));
 430 }
 431 ;
 432
 433/* K&R ---, C90 ---, C99 ---, C11 6.5.1.1, C18 6.5.1.1 */
 434generic_selection:
 435 T_GENERIC T_LPAREN assignment_expression T_COMMA
 436 generic_assoc_list T_RPAREN {
 437 /* generic selection requires C11 or later */
 438 c11ism(345);
 439 $$ = build_generic_selection($3, $5);
 440 }
 441 ;
 442
 443/* K&R ---, C90 ---, C99 ---, C11 6.5.1.1, C18 6.5.1.1 */
 444generic_assoc_list:
 445 generic_association
 446 | generic_assoc_list T_COMMA generic_association {
 447 $3->ga_prev = $1;
 448 $$ = $3;
 449 }
 450 ;
 451
 452/* K&R ---, C90 ---, C99 ---, C11 6.5.1.1, C18 6.5.1.1 */
 453generic_association:
 454 type_name T_COLON assignment_expression {
 455 $$ = getblk(sizeof(*$$));
 456 $$->ga_arg = $1;
 457 $$->ga_result = $3;
 458 }
 459 | T_DEFAULT T_COLON assignment_expression {
 460 $$ = getblk(sizeof(*$$));
 461 $$->ga_arg = NULL;
 462 $$->ga_result = $3;
 463 }
 464 ;
 465
 466/* K&R 7.1, C90 ???, C99 6.5.2, C11 6.5.2, C18 6.5.2 */
 467postfix_expression:
 468 primary_expression
 469 | postfix_expression T_LBRACK expr T_RBRACK {
 470 $$ = build(INDIR, build(PLUS, $1, $3), NULL);
 471 }
 472 | postfix_expression T_LPAREN T_RPAREN {
 473 $$ = new_function_call_node($1, NULL);
 474 }
 475 | postfix_expression T_LPAREN argument_expression_list T_RPAREN {
 476 $$ = new_function_call_node($1, $3);
 477 }
 478 | postfix_expression point_or_arrow T_NAME {
 479 if ($1 != NULL) {
 480 sym_t *msym;
 481 /*
 482 * XXX struct_or_union_member should be integrated
 483 * in build()
 484 */
 485 if ($2 == ARROW) {
 486 /*
 487 * must do this before struct_or_union_member
 488 * is called
 489 */
 490 $1 = cconv($1);
 491 }
 492 msym = struct_or_union_member($1, $2, getsym($3));
 493 $$ = build($2, $1, new_name_node(msym, 0));
 494 } else {
 495 $$ = NULL;
 496 }
 497 }
 498 | postfix_expression T_INCDEC {
 499 $$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL);
 500 }
 501 | T_LPAREN type_name T_RPAREN { /* C99 6.5.2.5 "Compound literals" */
 502 sym_t *tmp = mktempsym($2);
 503 begin_initialization(tmp);
 504 cgram_declare(tmp, true, NULL);
 505 } init_lbrace initializer_list comma_opt init_rbrace {
 506 if (!Sflag)
 507 /* compound literals are a C9X/GCC extension */
 508 gnuism(319);
 509 $$ = new_name_node(*current_initsym(), 0);
 510 end_initialization();
 511 }
 512 | T_LPAREN compound_statement_lbrace gcc_statement_expr_list {
 513 block_level--;
 514 mem_block_level--;
 515 begin_initialization(mktempsym(dup_type($3->tn_type)));
 516 mem_block_level++;
 517 block_level++;
 518 /* ({ }) is a GCC extension */
 519 gnuism(320);
 520 } compound_statement_rbrace T_RPAREN {
 521 $$ = new_name_node(*current_initsym(), 0);
 522 end_initialization();
380 } 523 }
381 ; 524 ;
382 525
 526comma_opt: /* helper for 'postfix_expression' */
 527 /* empty */
 528 | T_COMMA
 529 ;
 530
383/* 531/*
384 * On the top level, lint allows several forms of declarations that it doesn't 532 * The inner part of a GCC statement-expression of the form ({ ... }).
385 * allow in functions. For example, a single ';' is an empty declaration and 
386 * is supported by some compilers, but in a function it would be an empty 
387 * statement, not a declaration. This makes a difference in C90 mode, where 
388 * a statement must not be followed by a declaration. 
389 * 533 *
390 * See 'declaration' for all other declarations. 534 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
391 */ 535 */
392top_level_declaration: /* C99 6.9 calls this 'declaration' */ 536gcc_statement_expr_list:
393 T_SEMI { 537 gcc_statement_expr_item
394 if (sflag) { 538 | gcc_statement_expr_list gcc_statement_expr_item {
395 /* empty declaration */ 539 $$ = $2;
396 error(0); 
397 } else if (!tflag) { 
398 /* empty declaration */ 
399 warning(0); 
400 } 
401 } 540 }
402 | begin_type end_type notype_init_decls T_SEMI { 541 ;
403 if (sflag) { 542
404 /* old style declaration; add 'int' */ 543gcc_statement_expr_item:
405 error(1); 544 declaration {
406 } else if (!tflag) { 545 clear_warning_flags();
407 /* old style declaration; add 'int' */ 546 $$ = NULL;
408 warning(1); 
409 } 
410 } 547 }
411 | begin_type_declmods end_type T_SEMI { 548 | non_expr_statement {
412 if (dcs->d_scl == TYPEDEF) { 549 $$ = expr_zalloc_tnode();
413 /* typedef declares no type name */ 550 $$->tn_type = gettyp(VOID);
414 warning(72); 551 }
 552 | expr T_SEMI {
 553 if ($1 == NULL) { /* in case of syntax errors */
 554 $$ = expr_zalloc_tnode();
 555 $$->tn_type = gettyp(VOID);
415 } else { 556 } else {
416 /* empty declaration */ 557 /* XXX: do that only on the last name */
417 warning(2); 558 if ($1->tn_op == NAME)
 559 $1->tn_sym->s_used = true;
 560 $$ = $1;
 561 expr($1, false, false, false, false);
 562 seen_fallthrough = false;
418 } 563 }
419 } 564 }
420 | begin_type_declmods end_type notype_init_decls T_SEMI 565 ;
421 | begin_type_declaration_specifiers end_type T_SEMI { 566
422 if (dcs->d_scl == TYPEDEF) { 567point_or_arrow: /* helper for 'postfix_expression' */
423 /* typedef declares no type name */ 568 T_POINT {
424 warning(72); 569 symtyp = FMEMBER;
425 } else if (!dcs->d_nonempty_decl) { 570 $$ = POINT;
426 /* empty declaration */ 571 }
427 warning(2); 572 | T_ARROW {
 573 symtyp = FMEMBER;
 574 $$ = ARROW;
 575 }
 576 ;
 577
 578/* K&R 7.1, C90 ???, C99 6.5.2, C11 6.5.2, C18 6.5.2 */
 579argument_expression_list:
 580 expr %prec T_COMMA {
 581 $$ = new_function_argument_node(NULL, $1);
 582 }
 583 | argument_expression_list T_COMMA expr {
 584 $$ = new_function_argument_node($1, $3);
 585 }
 586 ;
 587
 588/* K&R 7.2, C90 ???, C99 6.5.3, C11 6.5.3, C18 6.5.3 */
 589unary_expression:
 590 postfix_expression
 591 | T_INCDEC unary_expression {
 592 $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL);
 593 }
 594 | T_AMPER cast_expression {
 595 $$ = build(ADDR, $2, NULL);
 596 }
 597 | T_ASTERISK cast_expression {
 598 $$ = build(INDIR, $2, NULL);
 599 }
 600 | T_ADDITIVE cast_expression {
 601 if (tflag && $1 == PLUS) {
 602 /* unary + is illegal in traditional C */
 603 warning(100);
428 } 604 }
 605 $$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL);
429 } 606 }
430 | begin_type_declaration_specifiers end_type type_init_decls T_SEMI 607 | T_COMPLEMENT cast_expression {
431 | error T_SEMI { 608 $$ = build(COMPL, $2, NULL);
432 global_clean_up(); 
433 } 609 }
434 | error T_RBRACE { 610 | T_LOGNOT cast_expression {
435 global_clean_up(); 611 $$ = build(NOT, $2, NULL);
 612 }
 613 | T_REAL cast_expression { /* GCC c_parser_unary_expression */
 614 $$ = build(REAL, $2, NULL);
 615 }
 616 | T_IMAG cast_expression { /* GCC c_parser_unary_expression */
 617 $$ = build(IMAG, $2, NULL);
 618 }
 619 | T_EXTENSION cast_expression { /* GCC c_parser_unary_expression */
 620 $$ = $2;
 621 }
 622 | T_SIZEOF unary_expression {
 623 $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type);
 624 if ($$ != NULL)
 625 check_expr_misc($2, false, false, false, false, false, true);
 626 }
 627 | T_SIZEOF T_LPAREN type_name T_RPAREN {
 628 $$ = build_sizeof($3);
 629 }
 630 /* K&R ---, C90 ---, C99 ---, C11 6.5.3, C18 6.5.3 */
 631 | T_ALIGNOF T_LPAREN type_name T_RPAREN {
 632 $$ = build_alignof($3);
 633 }
 634 ;
 635
 636/* The rule 'unary_operator' is inlined into unary_expression. */
 637
 638/* K&R 7.2, C90 ???, C99 6.5.4, C11 6.5.4, C18 6.5.4 */
 639cast_expression:
 640 unary_expression
 641 | T_LPAREN type_name T_RPAREN cast_expression {
 642 $$ = cast($4, $2);
436 } 643 }
437 ; 644 ;
438 645
439function_definition: /* C99 6.9.1 */ 646expr_opt:
440 func_decl { 647 /* empty */ {
441 if ($1->s_type->t_tspec != FUNC) { 648 $$ = NULL;
442 /* syntax error '%s' */ 
443 error(249, yytext); 
444 YYERROR; 
445 } 
446 if ($1->s_type->t_typedef) { 
447 /* ()-less function definition */ 
448 error(64); 
449 YYERROR; 
450 } 
451 funcdef($1); 
452 block_level++; 
453 begin_declaration_level(ARG); 
454 if (lwarn == LWARN_NONE) 
455 $1->s_used = true; 
456 } arg_declaration_list_opt { 
457 end_declaration_level(); 
458 block_level--; 
459 check_func_lint_directives(); 
460 check_func_old_style_arguments(); 
461 begin_control_statement(CS_FUNCTION_BODY); 
462 } compound_statement { 
463 funcend(); 
464 end_control_statement(CS_FUNCTION_BODY); 
465 } 649 }
 650 | expr
466 ; 651 ;
467 652
468func_decl: 653/* 'expression' also implements 'multiplicative_expression'. */
469 begin_type end_type notype_decl { 654/* 'expression' also implements 'additive_expression'. */
470 $$ = $3; 655/* 'expression' also implements 'shift_expression'. */
 656/* 'expression' also implements 'relational_expression'. */
 657/* 'expression' also implements 'equality_expression'. */
 658/* 'expression' also implements 'AND_expression'. */
 659/* 'expression' also implements 'exclusive_OR_expression'. */
 660/* 'expression' also implements 'inclusive_OR_expression'. */
 661/* 'expression' also implements 'logical_AND_expression'. */
 662/* 'expression' also implements 'logical_OR_expression'. */
 663/* 'expression' also implements 'conditional_expression'. */
 664/* 'expression' also implements 'assignment_expression'. */
 665/* TODO: rename to 'expression' */
 666/* K&R ???, C90 ???, C99 6.5.5 to 6.5.17, C11 ???, C18 ??? */
 667expr:
 668 expr T_ASTERISK expr {
 669 $$ = build(MULT, $1, $3);
471 } 670 }
472 | begin_type_declmods end_type notype_decl { 671 | expr T_MULTIPLICATIVE expr {
473 $$ = $3; 672 $$ = build($2, $1, $3);
474 } 673 }
475 | begin_type_declaration_specifiers end_type type_decl { 674 | expr T_ADDITIVE expr {
476 $$ = $3; 675 $$ = build($2, $1, $3);
 676 }
 677 | expr T_SHIFT expr {
 678 $$ = build($2, $1, $3);
 679 }
 680 | expr T_RELATIONAL expr {
 681 $$ = build($2, $1, $3);
 682 }
 683 | expr T_EQUALITY expr {
 684 $$ = build($2, $1, $3);
 685 }
 686 | expr T_AMPER expr {
 687 $$ = build(BITAND, $1, $3);
 688 }
 689 | expr T_BITXOR expr {
 690 $$ = build(BITXOR, $1, $3);
 691 }
 692 | expr T_BITOR expr {
 693 $$ = build(BITOR, $1, $3);
 694 }
 695 | expr T_LOGAND expr {
 696 $$ = build(LOGAND, $1, $3);
 697 }
 698 | expr T_LOGOR expr {
 699 $$ = build(LOGOR, $1, $3);
 700 }
 701 | expr T_QUEST expr T_COLON expr {
 702 $$ = build(QUEST, $1, build(COLON, $3, $5));
 703 }
 704 | expr T_ASSIGN expr {
 705 $$ = build(ASSIGN, $1, $3);
 706 }
 707 | expr T_OPASSIGN expr {
 708 $$ = build($2, $1, $3);
 709 }
 710 | expr T_COMMA expr {
 711 $$ = build(COMMA, $1, $3);
477 } 712 }
 713 | cast_expression
478 ; 714 ;
479 715
480arg_declaration_list_opt: /* C99 6.9.1p13 example 1 */ 716/* K&R ???, C90 ???, C99 6.5.16, C11 ???, C18 ??? */
 717assignment_expression:
 718 expr %prec T_ASSIGN
 719 ;
 720
 721constant_expr_list_opt: /* helper for gcc_attribute */
481 /* empty */ 722 /* empty */
482 | arg_declaration_list 723 | constant_expr_list
483 ; 724 ;
484 725
485arg_declaration_list: /* C99 6.9.1p13 example 1 */ 726constant_expr_list: /* helper for gcc_attribute */
486 arg_declaration 727 constant_expr
487 | arg_declaration_list arg_declaration 728 | constant_expr_list T_COMMA constant_expr
488 /* XXX or better "arg_declaration error" ? */ 
489 | error 
490 ; 729 ;
491 730
492/* 731constant_expr: /* C99 6.6 */
493 * "arg_declaration" is separated from "declaration" because it 732 expr %prec T_ASSIGN
494 * needs other error handling. 
495 */ 
496arg_declaration: 
497 begin_type_declmods end_type T_SEMI { 
498 /* empty declaration */ 
499 warning(2); 
500 } 
501 | begin_type_declmods end_type notype_init_decls T_SEMI 
502 | begin_type_declaration_specifiers end_type T_SEMI { 
503 if (!dcs->d_nonempty_decl) { 
504 /* empty declaration */ 
505 warning(2); 
506 } else { 
507 /* '%s' declared in argument declaration list */ 
508 warning(3, type_name(dcs->d_type)); 
509 } 
510 } 
511 | begin_type_declaration_specifiers end_type type_init_decls T_SEMI { 
512 if (dcs->d_nonempty_decl) { 
513 /* '%s' declared in argument declaration list */ 
514 warning(3, type_name(dcs->d_type)); 
515 } 
516 } 
517 | begin_type_declmods error 
518 | begin_type_declaration_specifiers error 
519 ; 733 ;
520 734
521declaration: /* C99 6.7 */ 735declaration: /* C99 6.7 */
522 begin_type_declmods end_type T_SEMI { 736 begin_type_declmods end_type T_SEMI {
523 if (dcs->d_scl == TYPEDEF) { 737 if (dcs->d_scl == TYPEDEF) {
524 /* typedef declares no type name */ 738 /* typedef declares no type name */
525 warning(72); 739 warning(72);
526 } else { 740 } else {
527 /* empty declaration */ 741 /* empty declaration */
528 warning(2); 742 warning(2);
529 } 743 }
530 } 744 }
531 | begin_type_declmods end_type notype_init_decls T_SEMI 745 | begin_type_declmods end_type notype_init_decls T_SEMI
532 | begin_type_declaration_specifiers end_type T_SEMI { 746 | begin_type_declaration_specifiers end_type T_SEMI {
533 if (dcs->d_scl == TYPEDEF) { 747 if (dcs->d_scl == TYPEDEF) {
534 /* typedef declares no type name */ 748 /* typedef declares no type name */
535 warning(72); 749 warning(72);
536 } else if (!dcs->d_nonempty_decl) { 750 } else if (!dcs->d_nonempty_decl) {
537 /* empty declaration */ 751 /* empty declaration */
538 warning(2); 752 warning(2);
539 } 753 }
540 } 754 }
541 | begin_type_declaration_specifiers end_type type_init_decls T_SEMI 755 | begin_type_declaration_specifiers end_type type_init_decls T_SEMI
542 | error T_SEMI 756 | error T_SEMI
543 ; 757 ;
544 758
545begin_type: 
546 /* empty */ { 
547 begin_type(); 
548 } 
549 ; 
550 
551end_type: 
552 /* empty */ { 
553 end_type(); 
554 } 
555 ; 
556 
557begin_type_declaration_specifiers: /* see C99 6.7 */ 759begin_type_declaration_specifiers: /* see C99 6.7 */
558 begin_type_typespec { 760 begin_type_typespec {
559 add_type($1); 761 add_type($1);
560 } 762 }
561 | begin_type_declmods type_specifier { 763 | begin_type_declmods type_specifier {
562 add_type($2); 764 add_type($2);
563 } 765 }
564 | type_attribute begin_type_declaration_specifiers 766 | type_attribute begin_type_declaration_specifiers
565 | begin_type_declaration_specifiers declmod 767 | begin_type_declaration_specifiers declmod
566 | begin_type_declaration_specifiers notype_type_specifier { 768 | begin_type_declaration_specifiers notype_type_specifier {
567 add_type($2); 769 add_type($2);
568 } 770 }
569 ; 771 ;
570 772
571begin_type_declmods: 773begin_type_declmods: /* see C99 6.7 */
572 begin_type T_QUAL { 774 begin_type T_QUAL {
573 add_qualifier($2); 775 add_qualifier($2);
574 } 776 }
575 | begin_type T_SCLASS { 777 | begin_type T_SCLASS {
576 add_storage_class($2); 778 add_storage_class($2);
577 } 779 }
578 | begin_type_declmods declmod 780 | begin_type_declmods declmod
579 ; 781 ;
580 782
581declmod: 783begin_type_noclass_declspecs:
582 T_QUAL { 784 begin_type_typespec {
583 add_qualifier($1); 785 add_type($1);
584 } 786 }
585 | T_SCLASS { 787 | type_attribute begin_type_noclass_declspecs
586 add_storage_class($1); 788 | begin_type_noclass_declmods type_specifier {
 789 add_type($2);
587 } 790 }
588 | type_attribute_list 791 | begin_type_noclass_declspecs T_QUAL {
 792 add_qualifier($2);
 793 }
 794 | begin_type_noclass_declspecs notype_type_specifier {
 795 add_type($2);
 796 }
 797 | begin_type_noclass_declspecs type_attribute
589 ; 798 ;
590 799
591begin_type_typespec: 800begin_type_typespec:
592 begin_type notype_type_specifier { 801 begin_type notype_type_specifier {
593 $$ = $2; 802 $$ = $2;
594 } 803 }
595 | T_TYPENAME begin_type { 804 | T_TYPENAME begin_type {
596 $$ = getsym($1)->s_type; 805 $$ = getsym($1)->s_type;
597 } 806 }
598 ; 807 ;
599 808
 809begin_type_noclass_declmods:
 810 begin_type T_QUAL {
 811 add_qualifier($2);
 812 }
 813 | begin_type_noclass_declmods T_QUAL {
 814 add_qualifier($2);
 815 }
 816 ;
 817
 818declmod:
 819 T_QUAL {
 820 add_qualifier($1);
 821 }
 822 | T_SCLASS {
 823 add_storage_class($1);
 824 }
 825 | type_attribute_list
 826 ;
 827
600type_attribute_list: 828type_attribute_list:
601 type_attribute 829 type_attribute
602 | type_attribute_list type_attribute 830 | type_attribute_list type_attribute
603 ; 831 ;
604 832
605type_attribute_opt: 833type_attribute_opt:
606 /* empty */ 834 /* empty */
607 | type_attribute 835 | type_attribute
608 ; 836 ;
609 837
610type_attribute: /* See C11 6.7 declaration-specifiers */ 838type_attribute: /* See C11 6.7 declaration-specifiers */
611 T_ATTRIBUTE T_LPAREN T_LPAREN { 839 T_ATTRIBUTE T_LPAREN T_LPAREN {
612 attron = true; 840 attron = true;
613 } gcc_attribute_spec_list { 841 } gcc_attribute_spec_list {
614 attron = false; 842 attron = false;
615 } T_RPAREN T_RPAREN 843 } T_RPAREN T_RPAREN
616 | T_ALIGNAS T_LPAREN align_as T_RPAREN 844 | T_ALIGNAS T_LPAREN align_as T_RPAREN
617 | T_PACKED { 845 | T_PACKED {
618 addpacked(); 846 addpacked();
619 } 847 }
620 | T_NORETURN 848 | T_NORETURN
621 ; 849 ;
622 850
 851align_as: /* See alignment-specifier in C11 6.7.5 */
 852 type_specifier
 853 | constant_expr
 854 ;
 855
 856begin_type:
 857 /* empty */ {
 858 begin_type();
 859 }
 860 ;
 861
 862end_type:
 863 /* empty */ {
 864 end_type();
 865 }
 866 ;
 867
623type_specifier: /* C99 6.7.2 */ 868type_specifier: /* C99 6.7.2 */
624 notype_type_specifier 869 notype_type_specifier
625 | T_TYPENAME { 870 | T_TYPENAME {
626 $$ = getsym($1)->s_type; 871 $$ = getsym($1)->s_type;
627 } 872 }
628 ; 873 ;
629 874
630notype_type_specifier: 875notype_type_specifier: /* see C99 6.7.2 */
631 T_TYPE { 876 T_TYPE {
632 $$ = gettyp($1); 877 $$ = gettyp($1);
633 } 878 }
634 | T_TYPEOF cast_expression { /* GCC extension */ 879 | T_TYPEOF cast_expression { /* GCC extension */
635 $$ = $2->tn_type; 880 $$ = $2->tn_type;
636 } 881 }
637 | struct_or_union_specifier { 882 | struct_or_union_specifier {
638 end_declaration_level(); 883 end_declaration_level();
639 $$ = $1; 884 $$ = $1;
640 } 885 }
641 | enum_specifier { 886 | enum_specifier {
642 end_declaration_level(); 887 end_declaration_level();
643 $$ = $1; 888 $$ = $1;
@@ -672,63 +917,63 @@ struct_or_union_specifier: /* C99 6.7.2. @@ -672,63 +917,63 @@ struct_or_union_specifier: /* C99 6.7.2.
672 ; 917 ;
673 918
674struct_or_union: /* C99 6.7.2.1 */ 919struct_or_union: /* C99 6.7.2.1 */
675 struct_or_union type_attribute 920 struct_or_union type_attribute
676 | T_STRUCT_OR_UNION { 921 | T_STRUCT_OR_UNION {
677 symtyp = FTAG; 922 symtyp = FTAG;
678 begin_declaration_level($1 == STRUCT ? MOS : MOU); 923 begin_declaration_level($1 == STRUCT ? MOS : MOU);
679 dcs->d_offset = 0; 924 dcs->d_offset = 0;
680 dcs->d_sou_align_in_bits = CHAR_SIZE; 925 dcs->d_sou_align_in_bits = CHAR_SIZE;
681 $$ = $1; 926 $$ = $1;
682 } 927 }
683 ; 928 ;
684 929
685braced_struct_declaration_list: 930braced_struct_declaration_list: /* see C99 6.7.2.1 */
686 struct_declaration_lbrace struct_declaration_list_with_rbrace { 931 struct_declaration_lbrace struct_declaration_list_with_rbrace {
687 $$ = $2; 932 $$ = $2;
688 } 933 }
689 ; 934 ;
690 935
691struct_declaration_lbrace: 936struct_declaration_lbrace: /* see C99 6.7.2.1 */
692 T_LBRACE { 937 T_LBRACE {
693 symtyp = FVFT; 938 symtyp = FVFT;
694 } 939 }
695 ; 940 ;
696 941
697struct_declaration_list_with_rbrace: 942struct_declaration_list_with_rbrace: /* see C99 6.7.2.1 */
698 struct_declaration_list T_SEMI T_RBRACE 943 struct_declaration_list T_SEMI T_RBRACE
699 | struct_declaration_list T_RBRACE { 944 | struct_declaration_list T_RBRACE {
700 if (sflag) { 945 if (sflag) {
701 /* syntax req. ';' after last struct/union member */ 946 /* syntax req. ';' after last struct/union member */
702 error(66); 947 error(66);
703 } else { 948 } else {
704 /* syntax req. ';' after last struct/union member */ 949 /* syntax req. ';' after last struct/union member */
705 warning(66); 950 warning(66);
706 } 951 }
707 $$ = $1; 952 $$ = $1;
708 } 953 }
709 | T_RBRACE { 954 | T_RBRACE {
710 $$ = NULL; 955 $$ = NULL;
711 } 956 }
712 ; 957 ;
713 958
714struct_declaration_list: 959struct_declaration_list: /* C99 6.7.2.1 */
715 struct_declaration 960 struct_declaration
716 | struct_declaration_list T_SEMI struct_declaration { 961 | struct_declaration_list T_SEMI struct_declaration {
717 $$ = lnklst($1, $3); 962 $$ = lnklst($1, $3);
718 } 963 }
719 ; 964 ;
720 965
721struct_declaration: 966struct_declaration: /* C99 6.7.2.1 */
722 begin_type_noclass_declmods end_type { 967 begin_type_noclass_declmods end_type {
723 /* too late, i know, but getsym() compensates it */ 968 /* too late, i know, but getsym() compensates it */
724 symtyp = FMEMBER; 969 symtyp = FMEMBER;
725 } notype_member_decls type_attribute_opt { 970 } notype_member_decls type_attribute_opt {
726 symtyp = FVFT; 971 symtyp = FVFT;
727 $$ = $4; 972 $$ = $4;
728 } 973 }
729 | begin_type_noclass_declspecs end_type { 974 | begin_type_noclass_declspecs end_type {
730 symtyp = FMEMBER; 975 symtyp = FMEMBER;
731 } type_member_decls type_attribute_opt { 976 } type_member_decls type_attribute_opt {
732 symtyp = FVFT; 977 symtyp = FVFT;
733 $$ = $4; 978 $$ = $4;
734 } 979 }
@@ -748,52 +993,27 @@ struct_declaration: @@ -748,52 +993,27 @@ struct_declaration:
748 anonymize($$); 993 anonymize($$);
749 } else { 994 } else {
750 /* syntax error '%s' */ 995 /* syntax error '%s' */
751 error(249, "unnamed member"); 996 error(249, "unnamed member");
752 $$ = NULL; 997 $$ = NULL;
753 } 998 }
754 } 999 }
755 | error { 1000 | error {
756 symtyp = FVFT; 1001 symtyp = FVFT;
757 $$ = NULL; 1002 $$ = NULL;
758 } 1003 }
759 ; 1004 ;
760 1005
761begin_type_noclass_declspecs: 1006/* TODO: rename 'decls' to 'declarators', everywhere. */
762 begin_type_typespec { 
763 add_type($1); 
764 } 
765 | type_attribute begin_type_noclass_declspecs 
766 | begin_type_noclass_declmods type_specifier { 
767 add_type($2); 
768 } 
769 | begin_type_noclass_declspecs T_QUAL { 
770 add_qualifier($2); 
771 } 
772 | begin_type_noclass_declspecs notype_type_specifier { 
773 add_type($2); 
774 } 
775 | begin_type_noclass_declspecs type_attribute 
776 ; 
777 
778begin_type_noclass_declmods: 
779 begin_type T_QUAL { 
780 add_qualifier($2); 
781 } 
782 | begin_type_noclass_declmods T_QUAL { 
783 add_qualifier($2); 
784 } 
785 ; 
786 
787notype_member_decls: 1007notype_member_decls:
788 notype_member_decl { 1008 notype_member_decl {
789 $$ = declarator_1_struct_union($1); 1009 $$ = declarator_1_struct_union($1);
790 } 1010 }
791 | notype_member_decls { 1011 | notype_member_decls {
792 symtyp = FMEMBER; 1012 symtyp = FMEMBER;
793 } T_COMMA type_member_decl { 1013 } T_COMMA type_member_decl {
794 $$ = lnklst($1, declarator_1_struct_union($4)); 1014 $$ = lnklst($1, declarator_1_struct_union($4));
795 } 1015 }
796 ; 1016 ;
797 1017
798type_member_decls: 1018type_member_decls:
799 type_member_decl { 1019 type_member_decl {
@@ -820,100 +1040,143 @@ notype_member_decl: @@ -820,100 +1040,143 @@ notype_member_decl:
820 1040
821type_member_decl: 1041type_member_decl:
822 type_decl 1042 type_decl
823 | type_decl T_COLON constant_expr { 1043 | type_decl T_COLON constant_expr {
824 $$ = bitfield($1, to_int_constant($3, true)); 1044 $$ = bitfield($1, to_int_constant($3, true));
825 } 1045 }
826 | { 1046 | {
827 symtyp = FVFT; 1047 symtyp = FVFT;
828 } T_COLON constant_expr { 1048 } T_COLON constant_expr {
829 $$ = bitfield(NULL, to_int_constant($3, true)); 1049 $$ = bitfield(NULL, to_int_constant($3, true));
830 } 1050 }
831 ; 1051 ;
832 1052
833enum_specifier: /* C99 6.7.2.2 */ 1053enum_specifier: /* C99 6.7.2.2 */
834 enum identifier_sym { 1054 enum identifier_sym {
835 $$ = mktag($2, ENUM, false, false); 1055 $$ = mktag($2, ENUM, false, false);
836 } 1056 }
837 | enum identifier_sym { 1057 | enum identifier_sym {
838 dcs->d_tagtyp = mktag($2, ENUM, true, false); 1058 dcs->d_tagtyp = mktag($2, ENUM, true, false);
839 } enum_declaration { 1059 } enum_declaration {
840 $$ = complete_tag_enum(dcs->d_tagtyp, $4); 1060 $$ = complete_tag_enum(dcs->d_tagtyp, $4);
841 } 1061 }
842 | enum { 1062 | enum {
843 dcs->d_tagtyp = mktag(NULL, ENUM, true, false); 1063 dcs->d_tagtyp = mktag(NULL, ENUM, true, false);
844 } enum_declaration { 1064 } enum_declaration {
845 $$ = complete_tag_enum(dcs->d_tagtyp, $3); 1065 $$ = complete_tag_enum(dcs->d_tagtyp, $3);
846 } 1066 }
847 | enum error { 1067 | enum error {
848 symtyp = FVFT; 1068 symtyp = FVFT;
849 $$ = gettyp(INT); 1069 $$ = gettyp(INT);
850 } 1070 }
851 ; 1071 ;
852 1072
853enum: 1073enum: /* helper for C99 6.7.2.2 */
854 T_ENUM { 1074 T_ENUM {
855 symtyp = FTAG; 1075 symtyp = FTAG;
856 begin_declaration_level(CTCONST); 1076 begin_declaration_level(CTCONST);
857 } 1077 }
858 ; 1078 ;
859 1079
860enum_declaration: 1080enum_declaration: /* helper for C99 6.7.2.2 */
861 enum_decl_lbrace enums_with_opt_comma T_RBRACE { 1081 enum_decl_lbrace enums_with_opt_comma T_RBRACE {
862 $$ = $2; 1082 $$ = $2;
863 } 1083 }
864 ; 1084 ;
865 1085
866enum_decl_lbrace: 1086enum_decl_lbrace: /* helper for C99 6.7.2.2 */
867 T_LBRACE { 1087 T_LBRACE {
868 symtyp = FVFT; 1088 symtyp = FVFT;
869 enumval = 0; 1089 enumval = 0;
870 } 1090 }
871 ; 1091 ;
872 1092
873enums_with_opt_comma: 1093enums_with_opt_comma: /* helper for C99 6.7.2.2 */
874 enumerator_list 1094 enumerator_list
875 | enumerator_list T_COMMA { 1095 | enumerator_list T_COMMA {
876 if (sflag) { 1096 if (sflag) {
877 /* trailing ',' prohibited in enum declaration */ 1097 /* trailing ',' prohibited in enum declaration */
878 error(54); 1098 error(54);
879 } else { 1099 } else {
880 /* trailing ',' prohibited in enum declaration */ 1100 /* trailing ',' prohibited in enum declaration */
881 c99ism(54); 1101 c99ism(54);
882 } 1102 }
883 $$ = $1; 1103 $$ = $1;
884 } 1104 }
885 ; 1105 ;
886 1106
887enumerator_list: 1107enumerator_list: /* C99 6.7.2.2 */
888 enumerator 1108 enumerator
889 | enumerator_list T_COMMA enumerator { 1109 | enumerator_list T_COMMA enumerator {
890 $$ = lnklst($1, $3); 1110 $$ = lnklst($1, $3);
 1111 }
 1112 | error {
 1113 $$ = NULL;
 1114 }
 1115 ;
 1116
 1117enumerator: /* C99 6.7.2.2 */
 1118 identifier_sym {
 1119 $$ = enumeration_constant($1, enumval, true);
 1120 }
 1121 | identifier_sym T_ASSIGN constant_expr {
 1122 $$ = enumeration_constant($1, to_int_constant($3, true), false);
 1123 }
 1124 ;
 1125
 1126type_qualifier: /* C99 6.7.3 */
 1127 T_QUAL {
 1128 $$ = xcalloc(1, sizeof(*$$));
 1129 if ($1 == CONST) {
 1130 $$->p_const = true;
 1131 } else if ($1 == VOLATILE) {
 1132 $$->p_volatile = true;
 1133 } else {
 1134 lint_assert($1 == RESTRICT || $1 == THREAD);
 1135 }
 1136 }
 1137 ;
 1138
 1139pointer: /* C99 6.7.5 */
 1140 asterisk type_qualifier_list_opt {
 1141 $$ = merge_qualified_pointer($1, $2);
 1142 }
 1143 | asterisk type_qualifier_list_opt pointer {
 1144 $$ = merge_qualified_pointer($1, $2);
 1145 $$ = merge_qualified_pointer($$, $3);
 1146 }
 1147 ;
 1148
 1149asterisk: /* helper for 'pointer' */
 1150 T_ASTERISK {
 1151 $$ = xcalloc(1, sizeof(*$$));
 1152 $$->p_pointer = true;
891 } 1153 }
892 | error { 1154 ;
 1155
 1156type_qualifier_list_opt: /* see C99 6.7.5 */
 1157 /* empty */ {
893 $$ = NULL; 1158 $$ = NULL;
894 } 1159 }
 1160 | type_qualifier_list
895 ; 1161 ;
896 1162
897enumerator: /* C99 6.7.2.2 */ 1163type_qualifier_list: /* C99 6.7.5 */
898 identifier_sym { 1164 type_qualifier
899 $$ = enumeration_constant($1, enumval, true); 1165 | type_qualifier_list type_qualifier {
900 } 1166 $$ = merge_qualified_pointer($1, $2);
901 | identifier_sym T_ASSIGN constant_expr { 
902 $$ = enumeration_constant($1, to_int_constant($3, true), false); 
903 } 1167 }
904 ; 1168 ;
905 1169
906 
907/* 1170/*
908 * For an explanation of 'notype' in the following rules, see the Bison 1171 * For an explanation of 'notype' in the following rules, see the Bison
909 * manual, section 7.1 "Semantic Info in Token Kinds". 1172 * manual, section 7.1 "Semantic Info in Token Kinds".
910 */ 1173 */
911 1174
912notype_init_decls: 1175notype_init_decls:
913 notype_init_decl 1176 notype_init_decl
914 | notype_init_decls T_COMMA type_init_decl 1177 | notype_init_decls T_COMMA type_init_decl
915 ; 1178 ;
916 1179
917type_init_decls: 1180type_init_decls:
918 type_init_decl 1181 type_init_decl
919 | type_init_decls T_COMMA type_init_decl 1182 | type_init_decls T_COMMA type_init_decl
@@ -1013,159 +1276,183 @@ type_direct_decl: @@ -1013,159 +1276,183 @@ type_direct_decl:
1013 * The two distinct rules type_param_decl and notype_param_decl avoid a 1276 * The two distinct rules type_param_decl and notype_param_decl avoid a
1014 * conflict in argument lists. A typename enclosed in parentheses is always 1277 * conflict in argument lists. A typename enclosed in parentheses is always
1015 * treated as a typename, not an argument name. For example, after 1278 * treated as a typename, not an argument name. For example, after
1016 * "typedef double a;", the declaration "f(int (a));" is interpreted as 1279 * "typedef double a;", the declaration "f(int (a));" is interpreted as
1017 * "f(int (double));", not "f(int a);". 1280 * "f(int (double));", not "f(int a);".
1018 */ 1281 */
1019type_param_decl: 1282type_param_decl:
1020 direct_param_decl 1283 direct_param_decl
1021 | pointer direct_param_decl { 1284 | pointer direct_param_decl {
1022 $$ = add_pointer($2, $1); 1285 $$ = add_pointer($2, $1);
1023 } 1286 }
1024 ; 1287 ;
1025 1288
1026array_size: 1289notype_param_decl:
1027 type_qualifier_list_opt T_SCLASS constant_expr { 1290 direct_notype_param_decl
1028 /* C11 6.7.6.3p7 */ 1291 | pointer direct_notype_param_decl {
1029 if ($2 != STATIC) 1292 $$ = add_pointer($2, $1);
1030 yyerror("Bad attribute"); 
1031 /* static array size is a C11 extension */ 
1032 c11ism(343); 
1033 $$ = $3; 
1034 } 1293 }
1035 | constant_expr 
1036 ; 1294 ;
1037 1295
1038direct_param_decl: 1296direct_param_decl:
1039 identifier type_attribute_list { 1297 identifier type_attribute_list {
1040 $$ = declarator_name(getsym($1)); 1298 $$ = declarator_name(getsym($1));
1041 } 1299 }
1042 | identifier { 1300 | identifier {
1043 $$ = declarator_name(getsym($1)); 1301 $$ = declarator_name(getsym($1));
1044 } 1302 }
1045 | T_LPAREN notype_param_decl T_RPAREN { 1303 | T_LPAREN notype_param_decl T_RPAREN {
1046 $$ = $2; 1304 $$ = $2;
1047 } 1305 }
1048 | direct_param_decl T_LBRACK T_RBRACK { 1306 | direct_param_decl T_LBRACK T_RBRACK {
1049 $$ = add_array($1, false, 0); 1307 $$ = add_array($1, false, 0);
1050 } 1308 }
1051 | direct_param_decl T_LBRACK array_size T_RBRACK { 1309 | direct_param_decl T_LBRACK array_size T_RBRACK {
1052 $$ = add_array($1, true, to_int_constant($3, false)); 1310 $$ = add_array($1, true, to_int_constant($3, false));
1053 } 1311 }
1054 | direct_param_decl param_list asm_or_symbolrename_opt { 1312 | direct_param_decl param_list asm_or_symbolrename_opt {
1055 $$ = add_function(symbolrename($1, $3), $2); 1313 $$ = add_function(symbolrename($1, $3), $2);
1056 end_declaration_level(); 1314 end_declaration_level();
1057 block_level--; 1315 block_level--;
1058 } 1316 }
1059 ; 1317 ;
1060 1318
1061notype_param_decl: 
1062 direct_notype_param_decl 
1063 | pointer direct_notype_param_decl { 
1064 $$ = add_pointer($2, $1); 
1065 } 
1066 ; 
1067 
1068direct_notype_param_decl: 1319direct_notype_param_decl:
1069 identifier { 1320 identifier {
1070 $$ = declarator_name(getsym($1)); 1321 $$ = declarator_name(getsym($1));
1071 } 1322 }
1072 | T_LPAREN notype_param_decl T_RPAREN { 1323 | T_LPAREN notype_param_decl T_RPAREN {
1073 $$ = $2; 1324 $$ = $2;
1074 } 1325 }
1075 | direct_notype_param_decl T_LBRACK T_RBRACK { 1326 | direct_notype_param_decl T_LBRACK T_RBRACK {
1076 $$ = add_array($1, false, 0); 1327 $$ = add_array($1, false, 0);
1077 } 1328 }
1078 | direct_notype_param_decl T_LBRACK array_size T_RBRACK { 1329 | direct_notype_param_decl T_LBRACK array_size T_RBRACK {
1079 $$ = add_array($1, true, to_int_constant($3, false)); 1330 $$ = add_array($1, true, to_int_constant($3, false));
1080 } 1331 }
1081 | direct_notype_param_decl param_list asm_or_symbolrename_opt { 1332 | direct_notype_param_decl param_list asm_or_symbolrename_opt {
1082 $$ = add_function(symbolrename($1, $3), $2); 1333 $$ = add_function(symbolrename($1, $3), $2);
1083 end_declaration_level(); 1334 end_declaration_level();
1084 block_level--; 1335 block_level--;
1085 } 1336 }
1086 ; 1337 ;
1087 1338
1088pointer: /* C99 6.7.5 */ 1339param_list:
1089 asterisk type_qualifier_list_opt { 1340 id_list_lparen identifier_list T_RPAREN {
1090 $$ = merge_qualified_pointer($1, $2); 1341 $$ = $2;
1091 } 
1092 | asterisk type_qualifier_list_opt pointer { 
1093 $$ = merge_qualified_pointer($1, $2); 
1094 $$ = merge_qualified_pointer($$, $3); 
1095 } 1342 }
 1343 | abstract_decl_param_list
1096 ; 1344 ;
1097 1345
1098asterisk: 1346id_list_lparen:
1099 T_ASTERISK { 1347 T_LPAREN {
1100 $$ = xcalloc(1, sizeof(*$$)); 1348 block_level++;
1101 $$->p_pointer = true; 1349 begin_declaration_level(PROTO_ARG);
1102 } 1350 }
1103 ; 1351 ;
1104 1352
1105type_qualifier_list_opt: 1353array_size:
1106 /* empty */ { 1354 type_qualifier_list_opt T_SCLASS constant_expr {
1107 $$ = NULL; 1355 /* C11 6.7.6.3p7 */
 1356 if ($2 != STATIC)
 1357 yyerror("Bad attribute");
 1358 /* static array size is a C11 extension */
 1359 c11ism(343);
 1360 $$ = $3;
1108 } 1361 }
1109 | type_qualifier_list 1362 | constant_expr
1110 ; 1363 ;
1111 1364
1112type_qualifier_list: /* C99 6.7.5 */ 1365identifier_list: /* C99 6.7.5 */
1113 type_qualifier 1366 T_NAME {
1114 | type_qualifier_list type_qualifier { 1367 $$ = old_style_function_name(getsym($1));
1115 $$ = merge_qualified_pointer($1, $2); 
1116 } 1368 }
1117 ; 1369 | identifier_list T_COMMA T_NAME {
1118 1370 $$ = lnklst($1, old_style_function_name(getsym($3)));
1119type_qualifier: 
1120 T_QUAL { 
1121 $$ = xcalloc(1, sizeof(*$$)); 
1122 if ($1 == CONST) { 
1123 $$->p_const = true; 
1124 } else if ($1 == VOLATILE) { 
1125 $$->p_volatile = true; 
1126 } else { 
1127 lint_assert($1 == RESTRICT || $1 == THREAD); 
1128 } 
1129 } 1371 }
 1372 | identifier_list error
1130 ; 1373 ;
1131 1374
1132align_as: /* See alignment-specifier in C11 6.7.5 */ 1375/* XXX: C99 requires an additional specifier-qualifier-list. */
1133 type_specifier 1376type_name: /* C99 6.7.6 */
1134 | constant_expr 1377 {
 1378 begin_declaration_level(ABSTRACT);
 1379 } abstract_declaration {
 1380 end_declaration_level();
 1381 $$ = $2->s_type;
 1382 }
1135 ; 1383 ;
1136 1384
1137param_list: 1385abstract_declaration:
1138 id_list_lparen identifier_list T_RPAREN { 1386 begin_type_noclass_declmods end_type {
1139 $$ = $2; 1387 $$ = declare_1_abstract(abstract_name());
 1388 }
 1389 | begin_type_noclass_declspecs end_type {
 1390 $$ = declare_1_abstract(abstract_name());
 1391 }
 1392 | begin_type_noclass_declmods end_type abstract_declarator {
 1393 $$ = declare_1_abstract($3);
 1394 }
 1395 | begin_type_noclass_declspecs end_type abstract_declarator {
 1396 $$ = declare_1_abstract($3);
1140 } 1397 }
1141 | abstract_decl_param_list 
1142 ; 1398 ;
1143 1399
1144id_list_lparen: 1400abstract_declarator: /* C99 6.7.6 */
1145 T_LPAREN { 1401 pointer {
1146 block_level++; 1402 $$ = add_pointer(abstract_name(), $1);
1147 begin_declaration_level(PROTO_ARG); 1403 }
 1404 | direct_abstract_declarator
 1405 | pointer direct_abstract_declarator {
 1406 $$ = add_pointer($2, $1);
 1407 }
 1408 | T_TYPEOF cast_expression { /* GCC extension */
 1409 $$ = mktempsym($2->tn_type);
1148 } 1410 }
1149 ; 1411 ;
1150 1412
1151identifier_list: 1413direct_abstract_declarator: /* C99 6.7.6 */
1152 T_NAME { 1414 T_LPAREN abstract_declarator T_RPAREN {
1153 $$ = old_style_function_name(getsym($1)); 1415 $$ = $2;
1154 } 1416 }
1155 | identifier_list T_COMMA T_NAME { 1417 | T_LBRACK T_RBRACK {
1156 $$ = lnklst($1, old_style_function_name(getsym($3))); 1418 $$ = add_array(abstract_name(), false, 0);
1157 } 1419 }
1158 | identifier_list error 1420 | T_LBRACK array_size T_RBRACK {
 1421 $$ = add_array(abstract_name(), true, to_int_constant($2, false));
 1422 }
 1423 | type_attribute direct_abstract_declarator {
 1424 $$ = $2;
 1425 }
 1426 | direct_abstract_declarator T_LBRACK T_RBRACK {
 1427 $$ = add_array($1, false, 0);
 1428 }
 1429 | direct_abstract_declarator T_LBRACK T_ASTERISK T_RBRACK { /* C99 */
 1430 $$ = add_array($1, false, 0);
 1431 }
 1432 | direct_abstract_declarator T_LBRACK array_size T_RBRACK {
 1433 $$ = add_array($1, true, to_int_constant($3, false));
 1434 }
 1435 | abstract_decl_param_list asm_or_symbolrename_opt {
 1436 $$ = add_function(symbolrename(abstract_name(), $2), $1);
 1437 end_declaration_level();
 1438 block_level--;
 1439 }
 1440 | direct_abstract_declarator abstract_decl_param_list asm_or_symbolrename_opt {
 1441 $$ = add_function(symbolrename($1, $3), $2);
 1442 end_declaration_level();
 1443 block_level--;
 1444 }
 1445 | direct_abstract_declarator type_attribute_list
1159 ; 1446 ;
1160 1447
1161abstract_decl_param_list: 1448abstract_decl_param_list:
1162 abstract_decl_lparen T_RPAREN type_attribute_opt { 1449 abstract_decl_lparen T_RPAREN type_attribute_opt {
1163 $$ = NULL; 1450 $$ = NULL;
1164 } 1451 }
1165 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN type_attribute_opt { 1452 | abstract_decl_lparen vararg_parameter_type_list T_RPAREN type_attribute_opt {
1166 dcs->d_proto = true; 1453 dcs->d_proto = true;
1167 $$ = $2; 1454 $$ = $2;
1168 } 1455 }
1169 | abstract_decl_lparen error T_RPAREN type_attribute_opt { 1456 | abstract_decl_lparen error T_RPAREN type_attribute_opt {
1170 $$ = NULL; 1457 $$ = NULL;
1171 } 1458 }
@@ -1187,26 +1474,27 @@ vararg_parameter_type_list: @@ -1187,26 +1474,27 @@ vararg_parameter_type_list:
1187 | T_ELLIPSIS { 1474 | T_ELLIPSIS {
1188 if (sflag) { 1475 if (sflag) {
1189 /* ANSI C requires formal parameter before '...' */ 1476 /* ANSI C requires formal parameter before '...' */
1190 error(84); 1477 error(84);
1191 } else if (!tflag) { 1478 } else if (!tflag) {
1192 /* ANSI C requires formal parameter before '...' */ 1479 /* ANSI C requires formal parameter before '...' */
1193 warning(84); 1480 warning(84);
1194 } 1481 }
1195 dcs->d_vararg = true; 1482 dcs->d_vararg = true;
1196 $$ = NULL; 1483 $$ = NULL;
1197 } 1484 }
1198 ; 1485 ;
1199 1486
 1487/* XXX: C99 6.7.5 defines the same name, but it looks different. */
1200parameter_type_list: 1488parameter_type_list:
1201 parameter_declaration 1489 parameter_declaration
1202 | parameter_type_list T_COMMA parameter_declaration { 1490 | parameter_type_list T_COMMA parameter_declaration {
1203 $$ = lnklst($1, $3); 1491 $$ = lnklst($1, $3);
1204 } 1492 }
1205 ; 1493 ;
1206 1494
1207/* XXX: C99 6.7.5 defines the same name, but it looks completely different. */ 1495/* XXX: C99 6.7.5 defines the same name, but it looks completely different. */
1208parameter_declaration: 1496parameter_declaration:
1209 begin_type_declmods end_type { 1497 begin_type_declmods end_type {
1210 $$ = declare_argument(abstract_name(), false); 1498 $$ = declare_argument(abstract_name(), false);
1211 } 1499 }
1212 | begin_type_declaration_specifiers end_type { 1500 | begin_type_declaration_specifiers end_type {
@@ -1223,56 +1511,43 @@ parameter_declaration: @@ -1223,56 +1511,43 @@ parameter_declaration:
1223 * This grammar realizes the second case. 1511 * This grammar realizes the second case.
1224 */ 1512 */
1225 | begin_type_declaration_specifiers end_type type_param_decl { 1513 | begin_type_declaration_specifiers end_type type_param_decl {
1226 $$ = declare_argument($3, false); 1514 $$ = declare_argument($3, false);
1227 } 1515 }
1228 | begin_type_declmods end_type abstract_declarator { 1516 | begin_type_declmods end_type abstract_declarator {
1229 $$ = declare_argument($3, false); 1517 $$ = declare_argument($3, false);
1230 } 1518 }
1231 | begin_type_declaration_specifiers end_type abstract_declarator { 1519 | begin_type_declaration_specifiers end_type abstract_declarator {
1232 $$ = declare_argument($3, false); 1520 $$ = declare_argument($3, false);
1233 } 1521 }
1234 ; 1522 ;
1235 1523
1236asm_or_symbolrename_opt: /* expect only one */ 
1237 /* empty */ { 
1238 $$ = NULL; 
1239 } 
1240 | T_ASM T_LPAREN T_STRING T_RPAREN { 
1241 freeyyv(&$3, T_STRING); 
1242 $$ = NULL; 
1243 } 
1244 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN { 
1245 $$ = $3; 
1246 } 
1247 ; 
1248 
1249initializer: /* C99 6.7.8 "Initialization" */ 1524initializer: /* C99 6.7.8 "Initialization" */
1250 expr %prec T_COMMA { 1525 expr %prec T_COMMA {
1251 init_expr($1); 1526 init_expr($1);
1252 } 1527 }
1253 | init_lbrace init_rbrace { 1528 | init_lbrace init_rbrace {
1254 /* XXX: Empty braces are not covered by C99 6.7.8. */ 1529 /* XXX: Empty braces are not covered by C99 6.7.8. */
1255 } 1530 }
1256 | init_lbrace initializer_list comma_opt init_rbrace 1531 | init_lbrace initializer_list comma_opt init_rbrace
1257 | error 1532 | error
1258 ; 1533 ;
1259 1534
1260initializer_list: /* C99 6.7.8 "Initialization" */ 1535initializer_list: /* C99 6.7.8 "Initialization" */
1261 initializer_list_item 1536 initializer_list_item
1262 | initializer_list T_COMMA initializer_list_item 1537 | initializer_list T_COMMA initializer_list_item
1263 ; 1538 ;
1264 1539
1265initializer_list_item: 1540initializer_list_item: /* helper */
1266 designation initializer 1541 designation initializer
1267 | initializer 1542 | initializer
1268 ; 1543 ;
1269 1544
1270designation: /* C99 6.7.8 "Initialization" */ 1545designation: /* C99 6.7.8 "Initialization" */
1271 designator_list T_ASSIGN 1546 designator_list T_ASSIGN
1272 | identifier T_COLON { 1547 | identifier T_COLON {
1273 /* GCC style struct or union member name in initializer */ 1548 /* GCC style struct or union member name in initializer */
1274 gnuism(315); 1549 gnuism(315);
1275 add_designator_member($1); 1550 add_designator_member($1);
1276 } 1551 }
1277 ; 1552 ;
1278 1553
@@ -1299,127 +1574,68 @@ designator: /* C99 6.7.8 "Initializati @@ -1299,127 +1574,68 @@ designator: /* C99 6.7.8 "Initializati
1299range: 1574range:
1300 constant_expr { 1575 constant_expr {
1301 $$.lo = to_int_constant($1, true); 1576 $$.lo = to_int_constant($1, true);
1302 $$.hi = $$.lo; 1577 $$.hi = $$.lo;
1303 } 1578 }
1304 | constant_expr T_ELLIPSIS constant_expr { 1579 | constant_expr T_ELLIPSIS constant_expr {
1305 $$.lo = to_int_constant($1, true); 1580 $$.lo = to_int_constant($1, true);
1306 $$.hi = to_int_constant($3, true); 1581 $$.hi = to_int_constant($3, true);
1307 /* initialization with '[a...b]' is a GCC extension */ 1582 /* initialization with '[a...b]' is a GCC extension */
1308 gnuism(340); 1583 gnuism(340);
1309 } 1584 }
1310 ; 1585 ;
1311 1586
1312init_lbrace: 1587init_lbrace: /* helper */
1313 T_LBRACE { 1588 T_LBRACE {
1314 init_lbrace(); 1589 init_lbrace();
1315 } 1590 }
1316 ; 1591 ;
1317 1592
1318init_rbrace: 1593init_rbrace: /* helper */
1319 T_RBRACE { 1594 T_RBRACE {
1320 init_rbrace(); 1595 init_rbrace();
1321 } 1596 }
1322 ; 1597 ;
1323 1598
1324type_name: /* C99 6.7.6 */ 1599asm_or_symbolrename_opt: /* GCC extensions */
1325 { 1600 /* empty */ {
1326 begin_declaration_level(ABSTRACT); 1601 $$ = NULL;
1327 } abstract_declaration { 
1328 end_declaration_level(); 
1329 $$ = $2->s_type; 
1330 } 
1331 ; 
1332 
1333abstract_declaration: 
1334 begin_type_noclass_declmods end_type { 
1335 $$ = declare_1_abstract(abstract_name()); 
1336 } 
1337 | begin_type_noclass_declspecs end_type { 
1338 $$ = declare_1_abstract(abstract_name()); 
1339 } 
1340 | begin_type_noclass_declmods end_type abstract_declarator { 
1341 $$ = declare_1_abstract($3); 
1342 } 
1343 | begin_type_noclass_declspecs end_type abstract_declarator { 
1344 $$ = declare_1_abstract($3); 
1345 } 
1346 ; 
1347 
1348abstract_declarator: /* C99 6.7.6 */ 
1349 pointer { 
1350 $$ = add_pointer(abstract_name(), $1); 
1351 } 1602 }
1352 | direct_abstract_declarator 1603 | T_ASM T_LPAREN T_STRING T_RPAREN {
1353 | pointer direct_abstract_declarator { 1604 freeyyv(&$3, T_STRING);
1354 $$ = add_pointer($2, $1); 1605 $$ = NULL;
1355 } 1606 }
1356 | T_TYPEOF cast_expression { /* GCC extension */ 1607 | T_SYMBOLRENAME T_LPAREN T_NAME T_RPAREN {
1357 $$ = mktempsym($2->tn_type); 1608 $$ = $3;
1358 } 1609 }
1359 ; 1610 ;
1360 1611
1361direct_abstract_declarator: /* C99 6.7.6 */ 1612statement: /* C99 6.8 */
1362 T_LPAREN abstract_declarator T_RPAREN { 1613 expression_statement
1363 $$ = $2; 1614 | non_expr_statement
1364 } 
1365 | T_LBRACK T_RBRACK { 
1366 $$ = add_array(abstract_name(), false, 0); 
1367 } 
1368 | T_LBRACK array_size T_RBRACK { 
1369 $$ = add_array(abstract_name(), true, to_int_constant($2, false)); 
1370 } 
1371 | type_attribute direct_abstract_declarator { 
1372 $$ = $2; 
1373 } 
1374 | direct_abstract_declarator T_LBRACK T_RBRACK { 
1375 $$ = add_array($1, false, 0); 
1376 } 
1377 | direct_abstract_declarator T_LBRACK T_ASTERISK T_RBRACK { /* C99 */ 
1378 $$ = add_array($1, false, 0); 
1379 } 
1380 | direct_abstract_declarator T_LBRACK array_size T_RBRACK { 
1381 $$ = add_array($1, true, to_int_constant($3, false)); 
1382 } 
1383 | abstract_decl_param_list asm_or_symbolrename_opt { 
1384 $$ = add_function(symbolrename(abstract_name(), $2), $1); 
1385 end_declaration_level(); 
1386 block_level--; 
1387 } 
1388 | direct_abstract_declarator abstract_decl_param_list asm_or_symbolrename_opt { 
1389 $$ = add_function(symbolrename($1, $3), $2); 
1390 end_declaration_level(); 
1391 block_level--; 
1392 } 
1393 | direct_abstract_declarator type_attribute_list 
1394 ; 1615 ;
1395 1616
1396non_expr_statement: 1617non_expr_statement: /* helper for C99 6.8 */
1397 type_attribute T_SEMI 1618 type_attribute T_SEMI
1398 | labeled_statement 1619 | labeled_statement
1399 | compound_statement 1620 | compound_statement
1400 | selection_statement 1621 | selection_statement
1401 | iteration_statement 1622 | iteration_statement
1402 | jump_statement { 1623 | jump_statement {
1403 seen_fallthrough = false; 1624 seen_fallthrough = false;
1404 } 1625 }
1405 | asm_statement 1626 | asm_statement
1406 ; 1627 ;
1407 1628
1408statement: /* C99 6.8 */ 
1409 expression_statement 
1410 | non_expr_statement 
1411 ; 
1412 
1413labeled_statement: /* C99 6.8.1 */ 1629labeled_statement: /* C99 6.8.1 */
1414 label type_attribute_opt statement 1630 label type_attribute_opt statement
1415 ; 1631 ;
1416 1632
1417label: 1633label:
1418 T_NAME T_COLON { 1634 T_NAME T_COLON {
1419 symtyp = FLABEL; 1635 symtyp = FLABEL;
1420 named_label(getsym($1)); 1636 named_label(getsym($1));
1421 } 1637 }
1422 | T_CASE constant_expr T_COLON { 1638 | T_CASE constant_expr T_COLON {
1423 case_label($2); 1639 case_label($2);
1424 seen_fallthrough = true; 1640 seen_fallthrough = true;
1425 } 1641 }
@@ -1524,32 +1740,26 @@ if_expr: /* see C99 6.8.4 */ @@ -1524,32 +1740,26 @@ if_expr: /* see C99 6.8.4 */
1524 T_IF T_LPAREN expr T_RPAREN { 1740 T_IF T_LPAREN expr T_RPAREN {
1525 if1($3); 1741 if1($3);
1526 clear_warning_flags(); 1742 clear_warning_flags();
1527 } 1743 }
1528 ; 1744 ;
1529 1745
1530switch_expr: /* see C99 6.8.4 */ 1746switch_expr: /* see C99 6.8.4 */
1531 T_SWITCH T_LPAREN expr T_RPAREN { 1747 T_SWITCH T_LPAREN expr T_RPAREN {
1532 switch1($3); 1748 switch1($3);
1533 clear_warning_flags(); 1749 clear_warning_flags();
1534 } 1750 }
1535 ; 1751 ;
1536 1752
1537do_statement: /* C99 6.8.5 */ 
1538 do statement { 
1539 clear_warning_flags(); 
1540 } 
1541 ; 
1542 
1543iteration_statement: /* C99 6.8.5 */ 1753iteration_statement: /* C99 6.8.5 */
1544 while_expr statement { 1754 while_expr statement {
1545 clear_warning_flags(); 1755 clear_warning_flags();
1546 while2(); 1756 while2();
1547 } 1757 }
1548 | while_expr error { 1758 | while_expr error {
1549 clear_warning_flags(); 1759 clear_warning_flags();
1550 while2(); 1760 while2();
1551 } 1761 }
1552 | do_statement do_while_expr { 1762 | do_statement do_while_expr {
1553 do2($2); 1763 do2($2);
1554 seen_fallthrough = false; 1764 seen_fallthrough = false;
1555 } 1765 }
@@ -1561,456 +1771,268 @@ iteration_statement: /* C99 6.8.5 */ @@ -1561,456 +1771,268 @@ iteration_statement: /* C99 6.8.5 */
1561 clear_warning_flags(); 1771 clear_warning_flags();
1562 for2(); 1772 for2();
1563 end_declaration_level(); 1773 end_declaration_level();
1564 block_level--; 1774 block_level--;
1565 } 1775 }
1566 | for_exprs error { 1776 | for_exprs error {
1567 clear_warning_flags(); 1777 clear_warning_flags();
1568 for2(); 1778 for2();
1569 end_declaration_level(); 1779 end_declaration_level();
1570 block_level--; 1780 block_level--;
1571 } 1781 }
1572 ; 1782 ;
1573 1783
1574while_expr: 1784while_expr: /* see C99 6.8.5 */
1575 T_WHILE T_LPAREN expr T_RPAREN { 1785 T_WHILE T_LPAREN expr T_RPAREN {
1576 while1($3); 1786 while1($3);
1577 clear_warning_flags(); 1787 clear_warning_flags();
1578 } 1788 }
1579 ; 1789 ;
1580 1790
 1791do_statement: /* see C99 6.8.5 */
 1792 do statement {
 1793 clear_warning_flags();
 1794 }
 1795 ;
 1796
1581do: /* see C99 6.8.5 */ 1797do: /* see C99 6.8.5 */
1582 T_DO { 1798 T_DO {
1583 do1(); 1799 do1();
1584 } 1800 }
1585 ; 1801 ;
1586 1802
1587do_while_expr: 1803do_while_expr: /* see C99 6.8.5 */
1588 T_WHILE T_LPAREN expr T_RPAREN T_SEMI { 1804 T_WHILE T_LPAREN expr T_RPAREN T_SEMI {
1589 $$ = $3; 1805 $$ = $3;
1590 } 1806 }
1591 ; 1807 ;
1592 1808
1593for_start: /* see C99 6.8.5 */ 1809for_start: /* see C99 6.8.5 */
1594 T_FOR T_LPAREN { 1810 T_FOR T_LPAREN {
1595 begin_declaration_level(AUTO); 1811 begin_declaration_level(AUTO);
1596 block_level++; 1812 block_level++;
1597 } 1813 }
1598 ; 1814 ;
1599 1815
1600for_exprs: /* see C99 6.8.5 */ 1816for_exprs: /* see C99 6.8.5 */
1601 for_start begin_type_declaration_specifiers end_type notype_init_decls T_SEMI 1817 for_start begin_type_declaration_specifiers end_type notype_init_decls T_SEMI
1602 expr_opt T_SEMI expr_opt T_RPAREN { 1818 expr_opt T_SEMI expr_opt T_RPAREN {
1603 /* variable declaration in for loop */ 1819 /* variable declaration in for loop */
1604 c99ism(325); 1820 c99ism(325);
1605 for1(NULL, $6, $8); 1821 for1(NULL, $6, $8);
1606 clear_warning_flags(); 1822 clear_warning_flags();
1607 } 1823 }
1608 | for_start expr_opt T_SEMI expr_opt T_SEMI expr_opt T_RPAREN { 1824 | for_start expr_opt T_SEMI expr_opt T_SEMI expr_opt T_RPAREN {
1609 for1($2, $4, $6); 1825 for1($2, $4, $6);
1610 clear_warning_flags(); 1826 clear_warning_flags();
1611 } 1827 }
1612 ; 1828 ;
1613 1829
1614jump_statement: /* C99 6.8.6 */ 1830jump_statement: /* C99 6.8.6 */
1615 goto identifier T_SEMI { 1831 goto identifier T_SEMI {
1616 do_goto(getsym($2)); 1832 do_goto(getsym($2));
1617 } 
1618 | goto error T_SEMI { 
1619 symtyp = FVFT; 
1620 } 
1621 | T_CONTINUE T_SEMI { 
1622 do_continue(); 
1623 } 
1624 | T_BREAK T_SEMI { 
1625 do_break(); 
1626 } 
1627 | T_RETURN T_SEMI { 
1628 do_return(NULL); 
1629 } 
1630 | T_RETURN expr T_SEMI { 
1631 do_return($2); 
1632 } 
1633 ; 
1634 
1635goto: 
1636 T_GOTO { 
1637 symtyp = FLABEL; 
1638 } 
1639 ; 
1640 
1641asm_statement: 
1642 T_ASM T_LPAREN read_until_rparen T_SEMI { 
1643 setasm(); 
1644 } 
1645 | T_ASM T_QUAL T_LPAREN read_until_rparen T_SEMI { 
1646 setasm(); 
1647 } 
1648 | T_ASM error 
1649 ; 
1650 
1651read_until_rparen: 
1652 /* empty */ { 
1653 ignore_up_to_rparen(); 
1654 } 
1655 ; 
1656 
1657constant_expr_list_opt: 
1658 /* empty */ 
1659 | constant_expr_list 
1660 ; 
1661 
1662constant_expr_list: 
1663 constant_expr 
1664 | constant_expr_list T_COMMA constant_expr 
1665 ; 
1666 
1667constant_expr: /* C99 6.6 */ 
1668 expr %prec T_ASSIGN 
1669 ; 
1670 
1671expr_opt: 
1672 /* empty */ { 
1673 $$ = NULL; 
1674 } 
1675 | expr 
1676 ; 
1677 
1678expr: /* C99 6.5 */ 
1679 expr T_ASTERISK expr { 
1680 $$ = build(MULT, $1, $3); 
1681 } 
1682 | expr T_MULTIPLICATIVE expr { 
1683 $$ = build($2, $1, $3); 
1684 } 
1685 | expr T_ADDITIVE expr { 
1686 $$ = build($2, $1, $3); 
1687 } 
1688 | expr T_SHIFT expr { 
1689 $$ = build($2, $1, $3); 
1690 } 
1691 | expr T_RELATIONAL expr { 
1692 $$ = build($2, $1, $3); 
1693 } 
1694 | expr T_EQUALITY expr { 
1695 $$ = build($2, $1, $3); 
1696 } 
1697 | expr T_AMPER expr { 
1698 $$ = build(BITAND, $1, $3); 
1699 } 
1700 | expr T_BITXOR expr { 
1701 $$ = build(BITXOR, $1, $3); 
1702 } 
1703 | expr T_BITOR expr { 
1704 $$ = build(BITOR, $1, $3); 
1705 } 
1706 | expr T_LOGAND expr { 
1707 $$ = build(LOGAND, $1, $3); 
1708 } 
1709 | expr T_LOGOR expr { 
1710 $$ = build(LOGOR, $1, $3); 
1711 } 
1712 | expr T_QUEST expr T_COLON expr { 
1713 $$ = build(QUEST, $1, build(COLON, $3, $5)); 
1714 } 
1715 | expr T_ASSIGN expr { 
1716 $$ = build(ASSIGN, $1, $3); 
1717 } 
1718 | expr T_OPASSIGN expr { 
1719 $$ = build($2, $1, $3); 
1720 } 
1721 | expr T_COMMA expr { 
1722 $$ = build(COMMA, $1, $3); 
1723 } 
1724 | cast_expression 
1725 ; 
1726 
1727assignment_expression: /* C99 6.5.16 */ 
1728 expr %prec T_ASSIGN 
1729 ; 
1730 
1731/* K&R 7.1, C90 ???, C99 6.5.1, C11 6.5.1, C18 6.5.1 */ 
1732primary_expression: 
1733 T_NAME { 
1734 /* XXX really necessary? */ 
1735 if (yychar < 0) 
1736 yychar = yylex(); 
1737 $$ = new_name_node(getsym($1), yychar); 
1738 } 
1739 | T_CON { 
1740 $$ = expr_new_constant(gettyp($1->v_tspec), $1); 
1741 } 
1742 | string { 
1743 $$ = new_string_node($1); 
1744 } 
1745 | T_LPAREN expr T_RPAREN { 
1746 if ($2 != NULL) 
1747 $2->tn_parenthesized = true; 
1748 $$ = $2; 
1749 } 
1750 | generic_selection 
1751 /* GCC primary-expression, see c_parser_postfix_expression */ 
1752 | T_BUILTIN_OFFSETOF T_LPAREN type_name T_COMMA identifier T_RPAREN { 
1753 symtyp = FMEMBER; 
1754 $$ = build_offsetof($3, getsym($5)); 
1755 } 
1756 ; 
1757 
1758/* K&R ---, C90 ---, C99 ---, C11 6.5.1.1, C18 6.5.1.1 */ 
1759generic_selection: 
1760 T_GENERIC T_LPAREN assignment_expression T_COMMA 
1761 generic_assoc_list T_RPAREN { 
1762 /* generic selection requires C11 or later */ 
1763 c11ism(345); 
1764 $$ = build_generic_selection($3, $5); 
1765 } 
1766 ; 
1767 
1768/* K&R ---, C90 ---, C99 ---, C11 6.5.1.1, C18 6.5.1.1 */ 
1769generic_assoc_list: 
1770 generic_association 
1771 | generic_assoc_list T_COMMA generic_association { 
1772 $3->ga_prev = $1; 
1773 $$ = $3; 
1774 } 
1775 ; 
1776 
1777/* K&R ---, C90 ---, C99 ---, C11 6.5.1.1, C18 6.5.1.1 */ 
1778generic_association: 
1779 type_name T_COLON assignment_expression { 
1780 $$ = getblk(sizeof(*$$)); 
1781 $$->ga_arg = $1; 
1782 $$->ga_result = $3; 
1783 } 
1784 | T_DEFAULT T_COLON assignment_expression { 
1785 $$ = getblk(sizeof(*$$)); 
1786 $$->ga_arg = NULL; 
1787 $$->ga_result = $3; 
1788 } 
1789 ; 
1790 
1791/* K&R 7.1, C90 ???, C99 6.5.2, C11 6.5.2, C18 6.5.2 */ 
1792postfix_expression: 
1793 primary_expression 
1794 | postfix_expression T_LBRACK expr T_RBRACK { 
1795 $$ = build(INDIR, build(PLUS, $1, $3), NULL); 
1796 } 
1797 | postfix_expression T_LPAREN T_RPAREN { 
1798 $$ = new_function_call_node($1, NULL); 
1799 } 
1800 | postfix_expression T_LPAREN argument_expression_list T_RPAREN { 
1801 $$ = new_function_call_node($1, $3); 
1802 } 
1803 | postfix_expression point_or_arrow T_NAME { 
1804 if ($1 != NULL) { 
1805 sym_t *msym; 
1806 /* 
1807 * XXX struct_or_union_member should be integrated 
1808 * in build() 
1809 */ 
1810 if ($2 == ARROW) { 
1811 /* 
1812 * must do this before struct_or_union_member 
1813 * is called 
1814 */ 
1815 $1 = cconv($1); 
1816 } 
1817 msym = struct_or_union_member($1, $2, getsym($3)); 
1818 $$ = build($2, $1, new_name_node(msym, 0)); 
1819 } else { 
1820 $$ = NULL; 
1821 } 
1822 } 
1823 | postfix_expression T_INCDEC { 
1824 $$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL); 
1825 } 
1826 | T_LPAREN type_name T_RPAREN { /* C99 6.5.2.5 "Compound literals" */ 
1827 sym_t *tmp = mktempsym($2); 
1828 begin_initialization(tmp); 
1829 cgram_declare(tmp, true, NULL); 
1830 } init_lbrace initializer_list comma_opt init_rbrace { 
1831 if (!Sflag) 
1832 /* compound literals are a C9X/GCC extension */ 
1833 gnuism(319); 
1834 $$ = new_name_node(*current_initsym(), 0); 
1835 end_initialization(); 
1836 } 
1837 | T_LPAREN compound_statement_lbrace gcc_statement_expr_list { 
1838 block_level--; 
1839 mem_block_level--; 
1840 begin_initialization(mktempsym(dup_type($3->tn_type))); 
1841 mem_block_level++; 
1842 block_level++; 
1843 /* ({ }) is a GCC extension */ 
1844 gnuism(320); 
1845 } compound_statement_rbrace T_RPAREN { 
1846 $$ = new_name_node(*current_initsym(), 0); 
1847 end_initialization(); 
1848 } 
1849 ; 
1850 
1851/* K&R 7.1, C90 ???, C99 6.5.2, C11 6.5.2, C18 6.5.2 */ 
1852argument_expression_list: 
1853 expr %prec T_COMMA { 
1854 $$ = new_function_argument_node(NULL, $1); 
1855 } 
1856 | argument_expression_list T_COMMA expr { 
1857 $$ = new_function_argument_node($1, $3); 
1858 } 
1859 ; 
1860 
1861/* K&R 7.2, C90 ???, C99 6.5.3, C11 6.5.3, C18 6.5.3 */ 
1862unary_expression: 
1863 postfix_expression 
1864 | T_INCDEC unary_expression { 
1865 $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL); 
1866 } 
1867 | T_AMPER cast_expression { 
1868 $$ = build(ADDR, $2, NULL); 
1869 } 
1870 | T_ASTERISK cast_expression { 
1871 $$ = build(INDIR, $2, NULL); 
1872 } 1833 }
1873 | T_ADDITIVE cast_expression { 1834 | goto error T_SEMI {
1874 if (tflag && $1 == PLUS) { 1835 symtyp = FVFT;
1875 /* unary + is illegal in traditional C */ 
1876 warning(100); 
1877 } 
1878 $$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL); 
1879 } 1836 }
1880 | T_COMPLEMENT cast_expression { 1837 | T_CONTINUE T_SEMI {
1881 $$ = build(COMPL, $2, NULL); 1838 do_continue();
1882 } 1839 }
1883 | T_LOGNOT cast_expression { 1840 | T_BREAK T_SEMI {
1884 $$ = build(NOT, $2, NULL); 1841 do_break();
1885 } 1842 }
1886 | T_REAL cast_expression { /* GCC c_parser_unary_expression */ 1843 | T_RETURN T_SEMI {
1887 $$ = build(REAL, $2, NULL); 1844 do_return(NULL);
1888 } 1845 }
1889 | T_IMAG cast_expression { /* GCC c_parser_unary_expression */ 1846 | T_RETURN expr T_SEMI {
1890 $$ = build(IMAG, $2, NULL); 1847 do_return($2);
1891 } 1848 }
1892 | T_EXTENSION cast_expression { /* GCC c_parser_unary_expression */ 1849 ;
1893 $$ = $2; 1850
 1851goto: /* see C99 6.8.6 */
 1852 T_GOTO {
 1853 symtyp = FLABEL;
1894 } 1854 }
1895 | T_SIZEOF unary_expression { 1855 ;
1896 $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type); 1856
1897 if ($$ != NULL) 1857asm_statement: /* GCC extension */
1898 check_expr_misc($2, false, false, false, false, false, true); 1858 T_ASM T_LPAREN read_until_rparen T_SEMI {
 1859 setasm();
1899 } 1860 }
1900 | T_SIZEOF T_LPAREN type_name T_RPAREN { 1861 | T_ASM T_QUAL T_LPAREN read_until_rparen T_SEMI {
1901 $$ = build_sizeof($3); 1862 setasm();
1902 } 1863 }
1903 /* K&R ---, C90 ---, C99 ---, C11 6.5.3, C18 6.5.3 */ 1864 | T_ASM error
1904 | T_ALIGNOF T_LPAREN type_name T_RPAREN { 1865 ;
1905 $$ = build_alignof($3); 1866
 1867read_until_rparen: /* helper for 'asm_statement' */
 1868 /* empty */ {
 1869 ignore_up_to_rparen();
1906 } 1870 }
1907 ; 1871 ;
1908 1872
1909/* K&R 7.2, C90 ???, C99 6.5.4, C11 6.5.4, C18 6.5.4 */ 1873translation_unit: /* C99 6.9 */
1910cast_expression: 1874 external_declaration
1911 unary_expression 1875 | translation_unit external_declaration
1912 | T_LPAREN type_name T_RPAREN cast_expression { 1876 ;
1913 $$ = cast($4, $2); 1877
 1878external_declaration: /* C99 6.9 */
 1879 asm_statement
 1880 | function_definition {
 1881 global_clean_up_decl(false);
 1882 clear_warning_flags();
 1883 }
 1884 | top_level_declaration {
 1885 global_clean_up_decl(false);
 1886 clear_warning_flags();
1914 } 1887 }
1915 ; 1888 ;
1916 1889
1917/* 1890/*
1918 * The inner part of a GCC statement-expression of the form ({ ... }). 1891 * On the top level, lint allows several forms of declarations that it doesn't
 1892 * allow in functions. For example, a single ';' is an empty declaration and
 1893 * is supported by some compilers, but in a function it would be an empty
 1894 * statement, not a declaration. This makes a difference in C90 mode, where
 1895 * a statement must not be followed by a declaration.
1919 * 1896 *
1920 * https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html 1897 * See 'declaration' for all other declarations.
1921 */ 1898 */
1922gcc_statement_expr_list: 1899top_level_declaration: /* C99 6.9 calls this 'declaration' */
1923 gcc_statement_expr_item 1900 T_SEMI {
1924 | gcc_statement_expr_list gcc_statement_expr_item { 1901 if (sflag) {
1925 $$ = $2; 1902 /* empty declaration */
1926 } 1903 error(0);
1927 ; 1904 } else if (!tflag) {
1928 1905 /* empty declaration */
1929gcc_statement_expr_item: 1906 warning(0);
1930 declaration { 1907 }
1931 clear_warning_flags(); 
1932 $$ = NULL; 
1933 } 1908 }
1934 | non_expr_statement { 1909 | begin_type end_type notype_init_decls T_SEMI {
1935 $$ = expr_zalloc_tnode(); 1910 if (sflag) {
1936 $$->tn_type = gettyp(VOID); 1911 /* old style declaration; add 'int' */
 1912 error(1);
 1913 } else if (!tflag) {
 1914 /* old style declaration; add 'int' */
 1915 warning(1);
 1916 }
1937 } 1917 }
1938 | expr T_SEMI { 1918 | begin_type_declmods end_type T_SEMI {
1939 if ($1 == NULL) { /* in case of syntax errors */ 1919 if (dcs->d_scl == TYPEDEF) {
1940 $$ = expr_zalloc_tnode(); 1920 /* typedef declares no type name */
1941 $$->tn_type = gettyp(VOID); 1921 warning(72);
1942 } else { 1922 } else {
1943 /* XXX: do that only on the last name */ 1923 /* empty declaration */
1944 if ($1->tn_op == NAME) 1924 warning(2);
1945 $1->tn_sym->s_used = true; 
1946 $$ = $1; 
1947 expr($1, false, false, false, false); 
1948 seen_fallthrough = false; 
1949 } 1925 }
1950 } 1926 }
1951 ; 1927 | begin_type_declmods end_type notype_init_decls T_SEMI
1952 1928 | begin_type_declaration_specifiers end_type T_SEMI {
1953string: 1929 if (dcs->d_scl == TYPEDEF) {
1954 T_STRING 1930 /* typedef declares no type name */
1955 | T_STRING string2 { 1931 warning(72);
1956 $$ = cat_strings($1, $2); 1932 } else if (!dcs->d_nonempty_decl) {
 1933 /* empty declaration */
 1934 warning(2);
 1935 }
 1936 }
 1937 | begin_type_declaration_specifiers end_type type_init_decls T_SEMI
 1938 | error T_SEMI {
 1939 global_clean_up();
 1940 }
 1941 | error T_RBRACE {
 1942 global_clean_up();
1957 } 1943 }
1958 ; 1944 ;
1959 1945
1960string2: 1946function_definition: /* C99 6.9.1 */
1961 T_STRING { 1947 func_decl {
1962 if (tflag) { 1948 if ($1->s_type->t_tspec != FUNC) {
1963 /* concatenated strings are illegal in traditional C */ 1949 /* syntax error '%s' */
1964 warning(219); 1950 error(249, yytext);
 1951 YYERROR;
1965 } 1952 }
1966 $$ = $1; 1953 if ($1->s_type->t_typedef) {
1967 } 1954 /* ()-less function definition */
1968 | string2 T_STRING { 1955 error(64);
1969 $$ = cat_strings($1, $2); 1956 YYERROR;
 1957 }
 1958 funcdef($1);
 1959 block_level++;
 1960 begin_declaration_level(ARG);
 1961 if (lwarn == LWARN_NONE)
 1962 $1->s_used = true;
 1963 } arg_declaration_list_opt {
 1964 end_declaration_level();
 1965 block_level--;
 1966 check_func_lint_directives();
 1967 check_func_old_style_arguments();
 1968 begin_control_statement(CS_FUNCTION_BODY);
 1969 } compound_statement {
 1970 funcend();
 1971 end_control_statement(CS_FUNCTION_BODY);
1970 } 1972 }
1971 ; 1973 ;
1972 1974
1973point_or_arrow: 1975func_decl:
1974 T_POINT { 1976 begin_type end_type notype_decl {
1975 symtyp = FMEMBER; 1977 $$ = $3;
1976 $$ = POINT; 
1977 } 1978 }
1978 | T_ARROW { 1979 | begin_type_declmods end_type notype_decl {
1979 symtyp = FMEMBER; 1980 $$ = $3;
1980 $$ = ARROW; 1981 }
 1982 | begin_type_declaration_specifiers end_type type_decl {
 1983 $$ = $3;
1981 } 1984 }
1982 ; 1985 ;
1983 1986
1984identifier_sym: 1987arg_declaration_list_opt: /* C99 6.9.1p13 example 1 */
1985 identifier { 1988 /* empty */
1986 $$ = getsym($1); 1989 | arg_declaration_list
1987 } 
1988 ; 1990 ;
1989 1991
1990identifier: /* C99 6.4.2.1 */ 1992arg_declaration_list: /* C99 6.9.1p13 example 1 */
1991 T_NAME { 1993 arg_declaration
1992 $$ = $1; 1994 | arg_declaration_list arg_declaration
1993 cgram_debug("name '%s'", $$->sb_name); 1995 /* XXX or better "arg_declaration error" ? */
1994 } 1996 | error
1995 | T_TYPENAME { 
1996 $$ = $1; 
1997 cgram_debug("typename '%s'", $$->sb_name); 
1998 } 
1999 ; 1997 ;
2000 1998
2001comma_opt: 1999/*
2002 /* empty */ 2000 * "arg_declaration" is separated from "declaration" because it
2003 | T_COMMA 2001 * needs other error handling.
 2002 */
 2003arg_declaration:
 2004 begin_type_declmods end_type T_SEMI {
 2005 /* empty declaration */
 2006 warning(2);
 2007 }
 2008 | begin_type_declmods end_type notype_init_decls T_SEMI
 2009 | begin_type_declaration_specifiers end_type T_SEMI {
 2010 if (!dcs->d_nonempty_decl) {
 2011 /* empty declaration */
 2012 warning(2);
 2013 } else {
 2014 /* '%s' declared in argument declaration list */
 2015 warning(3, type_name(dcs->d_type));
 2016 }
 2017 }
 2018 | begin_type_declaration_specifiers end_type type_init_decls T_SEMI {
 2019 if (dcs->d_nonempty_decl) {
 2020 /* '%s' declared in argument declaration list */
 2021 warning(3, type_name(dcs->d_type));
 2022 }
 2023 }
 2024 | begin_type_declmods error
 2025 | begin_type_declaration_specifiers error
2004 ; 2026 ;
2005 2027
2006gcc_attribute_spec_list: 2028gcc_attribute_spec_list:
2007 gcc_attribute_spec 2029 gcc_attribute_spec
2008 | gcc_attribute_spec_list T_COMMA gcc_attribute_spec 2030 | gcc_attribute_spec_list T_COMMA gcc_attribute_spec
2009 ; 2031 ;
2010 2032
2011gcc_attribute_spec: 2033gcc_attribute_spec:
2012 /* empty */ 2034 /* empty */
2013 | T_AT_ALWAYS_INLINE 2035 | T_AT_ALWAYS_INLINE
2014 | T_AT_ALIAS T_LPAREN string T_RPAREN 2036 | T_AT_ALIAS T_LPAREN string T_RPAREN
2015 | T_AT_ALIGNED T_LPAREN constant_expr T_RPAREN 2037 | T_AT_ALIGNED T_LPAREN constant_expr T_RPAREN
2016 | T_AT_ALIGNED 2038 | T_AT_ALIGNED