Sat Jul 10 19:29:28 2021 UTC ()
lint: rename grammar rules to align with C99

No functional change.


(rillig)
diff -r1.299 -r1.300 src/usr.bin/xlint/lint1/cgram.y

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

--- src/usr.bin/xlint/lint1/cgram.y 2021/07/10 18:56:54 1.299
+++ src/usr.bin/xlint/lint1/cgram.y 2021/07/10 19:29:28 1.300
@@ -1,15 +1,15 @@ @@ -1,15 +1,15 @@
1%{ 1%{
2/* $NetBSD: cgram.y,v 1.299 2021/07/10 18:56:54 rillig Exp $ */ 2/* $NetBSD: cgram.y,v 1.300 2021/07/10 19:29:28 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.299 2021/07/10 18:56:54 rillig Exp $"); 38__RCSID("$NetBSD: cgram.y,v 1.300 2021/07/10 19:29:28 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.
@@ -271,38 +271,38 @@ anonymize(sym_t *s) @@ -271,38 +271,38 @@ anonymize(sym_t *s)
271%left T_ADDITIVE 271%left T_ADDITIVE
272%left T_ASTERISK T_MULTIPLICATIVE 272%left T_ASTERISK T_MULTIPLICATIVE
273%right T_UNARY T_INCDEC T_SIZEOF T_REAL T_IMAG 273%right T_UNARY T_INCDEC T_SIZEOF T_REAL T_IMAG
274%left T_LPAREN T_LBRACK T_POINT T_ARROW 274%left T_LPAREN T_LBRACK T_POINT T_ARROW
275 275
276%token <y_name> T_NAME 276%token <y_name> T_NAME
277%token <y_name> T_TYPENAME 277%token <y_name> T_TYPENAME
278%token <y_val> T_CON 278%token <y_val> T_CON
279%token <y_string> T_STRING 279%token <y_string> T_STRING
280 280
281%type <y_sym> func_decl 281%type <y_sym> func_decl
282%type <y_sym> notype_decl 282%type <y_sym> notype_decl
283%type <y_sym> type_decl 283%type <y_sym> type_decl
284%type <y_type> typespec 284%type <y_type> type_specifier
285%type <y_type> notype_typespec 285%type <y_type> notype_type_specifier
286%type <y_type> struct_spec 286%type <y_type> struct_or_union_specifier
287%type <y_type> enum_spec 287%type <y_type> enum_specifier
288%type <y_sym> struct_tag 288%type <y_sym> struct_tag
289%type <y_sym> enum_tag 289%type <y_sym> enum_tag
290%type <y_tspec> struct 290%type <y_tspec> struct_or_union
291%type <y_sym> struct_declaration 291%type <y_sym> braced_struct_declaration_list
292%type <y_name> identifier 292%type <y_name> identifier
293%type <y_sym> member_declaration_list_semi 293%type <y_sym> struct_declaration_list_semi
294%type <y_sym> member_declaration_list 294%type <y_sym> struct_declaration_list
295%type <y_sym> member_declaration 295%type <y_sym> struct_declaration
296%type <y_sym> notype_member_decls 296%type <y_sym> notype_member_decls
297%type <y_sym> type_member_decls 297%type <y_sym> type_member_decls
298%type <y_sym> notype_member_decl 298%type <y_sym> notype_member_decl
299%type <y_sym> type_member_decl 299%type <y_sym> type_member_decl
300%type <y_tnode> constant_expr 300%type <y_tnode> constant_expr
301%type <y_tnode> array_size 301%type <y_tnode> array_size
302%type <y_sym> enum_declaration 302%type <y_sym> enum_declaration
303%type <y_sym> enumerator_list 303%type <y_sym> enumerator_list
304%type <y_sym> enumerator 304%type <y_sym> enumerator
305%type <y_sym> enumeration_constant 305%type <y_sym> enumeration_constant
306%type <y_sym> notype_direct_decl 306%type <y_sym> notype_direct_decl
307%type <y_sym> type_direct_decl 307%type <y_sym> type_direct_decl
308%type <y_qual_ptr> pointer 308%type <y_qual_ptr> pointer
@@ -525,161 +525,162 @@ declaration_noerror: /* see C99 6.7 'de @@ -525,161 +525,162 @@ declaration_noerror: /* see C99 6.7 'de
525begin_type: 525begin_type:
526 /* empty */ { 526 /* empty */ {
527 begin_type(); 527 begin_type();
528 } 528 }
529 ; 529 ;
530 530
531end_type: 531end_type:
532 /* empty */ { 532 /* empty */ {
533 end_type(); 533 end_type();
534 } 534 }
535 ; 535 ;
536 536
537declaration_specifiers: /* C99 6.7 */ 537declaration_specifiers: /* C99 6.7 */
538 add_typespec 538 add_type_specifier
539 | declmods add_typespec 539 | declmods add_type_specifier
540 | type_attribute declaration_specifiers 540 | type_attribute declaration_specifiers
541 | declaration_specifiers declmod 541 | declaration_specifiers declmod
542 | declaration_specifiers add_notype_typespec 542 | declaration_specifiers add_notype_type_specifier
543 ; 543 ;
544 544
545declmods: 545declmods:
546 qualifier_or_storage_class 546 qualifier_or_storage_class
547 | declmods declmod 547 | declmods declmod
548 ; 548 ;
549 549
550declmod: 550declmod:
551 qualifier_or_storage_class 551 qualifier_or_storage_class
552 | type_attribute 552 | type_attribute
553 ; 553 ;
554 554
555qualifier_or_storage_class: 555qualifier_or_storage_class:
556 add_type_qualifier 556 add_type_qualifier
557 | T_SCLASS { 557 | T_SCLASS {
558 add_storage_class($1); 558 add_storage_class($1);
559 } 559 }
560 ; 560 ;
561 561
562add_typespec: 562add_type_specifier:
563 typespec { 563 type_specifier {
564 add_type($1); 564 add_type($1);
565 } 565 }
566 ; 566 ;
567 567
568typespec: 568type_specifier: /* C99 6.7.2 */
569 notype_typespec 569 notype_type_specifier
570 | T_TYPENAME { 570 | T_TYPENAME {
571 $$ = getsym($1)->s_type; 571 $$ = getsym($1)->s_type;
572 } 572 }
573 ; 573 ;
574 574
575add_notype_typespec: 575add_notype_type_specifier:
576 notype_typespec { 576 notype_type_specifier {
577 add_type($1); 577 add_type($1);
578 } 578 }
579 ; 579 ;
580 580
581notype_typespec: 581/* Like type_specifier, but without typedef-name. */
 582notype_type_specifier:
582 T_TYPE { 583 T_TYPE {
583 $$ = gettyp($1); 584 $$ = gettyp($1);
584 } 585 }
585 | T_TYPEOF term { 586 | T_TYPEOF term {
586 $$ = $2->tn_type; 587 $$ = $2->tn_type;
587 } 588 }
588 | struct_spec { 589 | struct_or_union_specifier {
589 end_declaration_level(); 590 end_declaration_level();
590 $$ = $1; 591 $$ = $1;
591 } 592 }
592 | enum_spec { 593 | enum_specifier {
593 end_declaration_level(); 594 end_declaration_level();
594 $$ = $1; 595 $$ = $1;
595 } 596 }
596 ; 597 ;
597 598
598struct_spec: 599struct_or_union_specifier: /* C99 6.7.2.1 */
599 struct struct_tag { 600 struct_or_union struct_tag {
600 /* 601 /*
601 * STDC requires that "struct a;" always introduces 602 * STDC requires that "struct a;" always introduces
602 * a new tag if "a" is not declared at current level 603 * a new tag if "a" is not declared at current level
603 * 604 *
604 * yychar is valid because otherwise the parser would not 605 * yychar is valid because otherwise the parser would not
605 * have been able to decide if it must shift or reduce 606 * have been able to decide if it must shift or reduce
606 */ 607 */
607 $$ = mktag($2, $1, false, yychar == T_SEMI); 608 $$ = mktag($2, $1, false, yychar == T_SEMI);
608 } 609 }
609 | struct struct_tag { 610 | struct_or_union struct_tag {
610 dcs->d_tagtyp = mktag($2, $1, true, false); 611 dcs->d_tagtyp = mktag($2, $1, true, false);
611 } struct_declaration { 612 } braced_struct_declaration_list {
612 $$ = complete_tag_struct_or_union(dcs->d_tagtyp, $4); 613 $$ = complete_tag_struct_or_union(dcs->d_tagtyp, $4);
613 } 614 }
614 | struct { 615 | struct_or_union {
615 dcs->d_tagtyp = mktag(NULL, $1, true, false); 616 dcs->d_tagtyp = mktag(NULL, $1, true, false);
616 } struct_declaration { 617 } braced_struct_declaration_list {
617 $$ = complete_tag_struct_or_union(dcs->d_tagtyp, $3); 618 $$ = complete_tag_struct_or_union(dcs->d_tagtyp, $3);
618 } 619 }
619 | struct error { 620 | struct_or_union error {
620 symtyp = FVFT; 621 symtyp = FVFT;
621 $$ = gettyp(INT); 622 $$ = gettyp(INT);
622 } 623 }
623 ; 624 ;
624 625
625struct: 626struct_or_union: /* C99 6.7.2.1 */
626 T_STRUCT_OR_UNION { 627 T_STRUCT_OR_UNION {
627 symtyp = FTAG; 628 symtyp = FTAG;
628 begin_declaration_level($1 == STRUCT ? MOS : MOU); 629 begin_declaration_level($1 == STRUCT ? MOS : MOU);
629 dcs->d_offset = 0; 630 dcs->d_offset = 0;
630 dcs->d_sou_align_in_bits = CHAR_SIZE; 631 dcs->d_sou_align_in_bits = CHAR_SIZE;
631 } type_attribute_list_opt 632 } type_attribute_list_opt
632 ; 633 ;
633 634
634struct_tag: 635struct_tag:
635 identifier { 636 identifier {
636 $$ = getsym($1); 637 $$ = getsym($1);
637 } 638 }
638 ; 639 ;
639 640
640struct_declaration: 641braced_struct_declaration_list:
641 T_LBRACE { 642 T_LBRACE {
642 symtyp = FVFT; 643 symtyp = FVFT;
643 } member_declaration_list_semi T_RBRACE { 644 } struct_declaration_list_semi T_RBRACE {
644 $$ = $3; 645 $$ = $3;
645 } 646 }
646 ; 647 ;
647 648
648member_declaration_list_semi: 649struct_declaration_list_semi:
649 /* empty */ { 650 /* empty */ {
650 $$ = NULL; 651 $$ = NULL;
651 } 652 }
652 | member_declaration_list T_SEMI 653 | struct_declaration_list T_SEMI
653 | member_declaration_list { 654 | struct_declaration_list {
654 if (sflag) { 655 if (sflag) {
655 /* syntax req. ';' after last struct/union member */ 656 /* syntax req. ';' after last struct/union member */
656 error(66); 657 error(66);
657 } else { 658 } else {
658 /* syntax req. ';' after last struct/union member */ 659 /* syntax req. ';' after last struct/union member */
659 warning(66); 660 warning(66);
660 } 661 }
661 $$ = $1; 662 $$ = $1;
662 } 663 }
663 ; 664 ;
664 665
665member_declaration_list: 666struct_declaration_list:
666 member_declaration 667 struct_declaration
667 | member_declaration_list T_SEMI member_declaration { 668 | struct_declaration_list T_SEMI struct_declaration {
668 $$ = lnklst($1, $3); 669 $$ = lnklst($1, $3);
669 } 670 }
670 ; 671 ;
671 672
672member_declaration: 673struct_declaration:
673 begin_type add_type_qualifier_list end_type { 674 begin_type add_type_qualifier_list end_type {
674 /* too late, i know, but getsym() compensates it */ 675 /* too late, i know, but getsym() compensates it */
675 symtyp = FMEMBER; 676 symtyp = FMEMBER;
676 } notype_member_decls type_attribute_opt { 677 } notype_member_decls type_attribute_opt {
677 symtyp = FVFT; 678 symtyp = FVFT;
678 $$ = $5; 679 $$ = $5;
679 } 680 }
680 | begin_type noclass_declspecs end_type { 681 | begin_type noclass_declspecs end_type {
681 symtyp = FMEMBER; 682 symtyp = FMEMBER;
682 } type_member_decls type_attribute_opt { 683 } type_member_decls type_attribute_opt {
683 symtyp = FVFT; 684 symtyp = FVFT;
684 $$ = $5; 685 $$ = $5;
685 } 686 }
@@ -705,30 +706,30 @@ member_declaration: @@ -705,30 +706,30 @@ member_declaration:
705 } 706 }
706 | error { 707 | error {
707 symtyp = FVFT; 708 symtyp = FVFT;
708 $$ = NULL; 709 $$ = NULL;
709 } 710 }
710 ; 711 ;
711 712
712noclass_declspecs: 713noclass_declspecs:
713 noclass_declspecs_postfix 714 noclass_declspecs_postfix
714 | type_attribute noclass_declspecs_postfix 715 | type_attribute noclass_declspecs_postfix
715 ; 716 ;
716 717
717noclass_declspecs_postfix: 718noclass_declspecs_postfix:
718 add_typespec 719 add_type_specifier
719 | add_type_qualifier_list add_typespec 720 | add_type_qualifier_list add_type_specifier
720 | noclass_declspecs_postfix add_type_qualifier 721 | noclass_declspecs_postfix add_type_qualifier
721 | noclass_declspecs_postfix add_notype_typespec 722 | noclass_declspecs_postfix add_notype_type_specifier
722 | noclass_declspecs_postfix type_attribute 723 | noclass_declspecs_postfix type_attribute
723 ; 724 ;
724 725
725add_type_qualifier_list: 726add_type_qualifier_list:
726 add_type_qualifier 727 add_type_qualifier
727 | add_type_qualifier_list add_type_qualifier 728 | add_type_qualifier_list add_type_qualifier
728 ; 729 ;
729 730
730add_type_qualifier: 731add_type_qualifier:
731 T_QUAL { 732 T_QUAL {
732 add_qualifier($1); 733 add_qualifier($1);
733 } 734 }
734 ; 735 ;
@@ -769,27 +770,27 @@ notype_member_decl: @@ -769,27 +770,27 @@ notype_member_decl:
769 770
770type_member_decl: 771type_member_decl:
771 type_decl 772 type_decl
772 | type_decl T_COLON constant_expr { 773 | type_decl T_COLON constant_expr {
773 $$ = bitfield($1, to_int_constant($3, true)); 774 $$ = bitfield($1, to_int_constant($3, true));
774 } 775 }
775 | { 776 | {
776 symtyp = FVFT; 777 symtyp = FVFT;
777 } T_COLON constant_expr { 778 } T_COLON constant_expr {
778 $$ = bitfield(NULL, to_int_constant($3, true)); 779 $$ = bitfield(NULL, to_int_constant($3, true));
779 } 780 }
780 ; 781 ;
781 782
782enum_spec: 783enum_specifier: /* C99 6.7.2.2 */
783 enum enum_tag { 784 enum enum_tag {
784 $$ = mktag($2, ENUM, false, false); 785 $$ = mktag($2, ENUM, false, false);
785 } 786 }
786 | enum enum_tag { 787 | enum enum_tag {
787 dcs->d_tagtyp = mktag($2, ENUM, true, false); 788 dcs->d_tagtyp = mktag($2, ENUM, true, false);
788 } enum_declaration { 789 } enum_declaration {
789 $$ = complete_tag_enum(dcs->d_tagtyp, $4); 790 $$ = complete_tag_enum(dcs->d_tagtyp, $4);
790 } 791 }
791 | enum { 792 | enum {
792 dcs->d_tagtyp = mktag(NULL, ENUM, true, false); 793 dcs->d_tagtyp = mktag(NULL, ENUM, true, false);
793 } enum_declaration { 794 } enum_declaration {
794 $$ = complete_tag_enum(dcs->d_tagtyp, $3); 795 $$ = complete_tag_enum(dcs->d_tagtyp, $3);
795 } 796 }
@@ -1061,27 +1062,27 @@ type_qualifier: @@ -1061,27 +1062,27 @@ type_qualifier:
1061 T_QUAL { 1062 T_QUAL {
1062 $$ = xcalloc(1, sizeof(*$$)); 1063 $$ = xcalloc(1, sizeof(*$$));
1063 if ($1 == CONST) { 1064 if ($1 == CONST) {
1064 $$->p_const = true; 1065 $$->p_const = true;
1065 } else if ($1 == VOLATILE) { 1066 } else if ($1 == VOLATILE) {
1066 $$->p_volatile = true; 1067 $$->p_volatile = true;
1067 } else { 1068 } else {
1068 lint_assert($1 == RESTRICT || $1 == THREAD); 1069 lint_assert($1 == RESTRICT || $1 == THREAD);
1069 } 1070 }
1070 } 1071 }
1071 ; 1072 ;
1072 1073
1073align_as: /* See alignment-specifier in C11 6.7.5 */ 1074align_as: /* See alignment-specifier in C11 6.7.5 */
1074 typespec 1075 type_specifier
1075 | constant_expr 1076 | constant_expr
1076 ; 1077 ;
1077 1078
1078param_list: 1079param_list:
1079 id_list_lparen identifier_list T_RPAREN { 1080 id_list_lparen identifier_list T_RPAREN {
1080 $$ = $2; 1081 $$ = $2;
1081 } 1082 }
1082 | abstract_decl_param_list 1083 | abstract_decl_param_list
1083 ; 1084 ;
1084 1085
1085id_list_lparen: 1086id_list_lparen:
1086 T_LPAREN { 1087 T_LPAREN {
1087 block_level++; 1088 block_level++;
@@ -1404,43 +1405,43 @@ compound_statement_rbrace: @@ -1404,43 +1405,43 @@ compound_statement_rbrace:
1404 end_declaration_level(); 1405 end_declaration_level();
1405 freeblk(); 1406 freeblk();
1406 mem_block_level--; 1407 mem_block_level--;
1407 block_level--; 1408 block_level--;
1408 seen_fallthrough = false; 1409 seen_fallthrough = false;
1409 } 1410 }
1410 ; 1411 ;
1411 1412
1412block_item_list_opt: /* C99 6.8.2 */ 1413block_item_list_opt: /* C99 6.8.2 */
1413 /* empty */ 1414 /* empty */
1414 | block_item_list 1415 | block_item_list
1415 ; 1416 ;
1416 1417
1417block_item_list: 1418block_item_list: /* C99 6.8.2 */
1418 block_item 1419 block_item
1419 | block_item_list block_item { 1420 | block_item_list block_item {
1420 if (!Sflag && $1 && !$2) 1421 if (!Sflag && $1 && !$2)
1421 /* declarations after statements is a C99 feature */ 1422 /* declarations after statements is a C99 feature */
1422 c99ism(327); 1423 c99ism(327);
1423 $$ = $1 || $2; 1424 $$ = $1 || $2;
1424 } 1425 }
1425 ; 1426 ;
1426 1427
1427block_item: 1428block_item: /* C99 6.8.2 */
1428 statement { 1429 declaration {
1429 $$ = true; 1430 $$ = false;
1430 restore_warning_flags(); 1431 restore_warning_flags();
1431 } 1432 }
1432 | declaration { 1433 | statement {
1433 $$ = false; 1434 $$ = true;
1434 restore_warning_flags(); 1435 restore_warning_flags();
1435 } 1436 }
1436 ; 1437 ;
1437 1438
1438expression_statement: /* C99 6.8.3 */ 1439expression_statement: /* C99 6.8.3 */
1439 expr T_SEMI { 1440 expr T_SEMI {
1440 expr($1, false, false, false, false); 1441 expr($1, false, false, false, false);
1441 seen_fallthrough = false; 1442 seen_fallthrough = false;
1442 } 1443 }
1443 | T_SEMI { 1444 | T_SEMI {
1444 seen_fallthrough = false; 1445 seen_fallthrough = false;
1445 } 1446 }
1446 ; 1447 ;