| @@ -1,15 +1,15 @@ | | | @@ -1,15 +1,15 @@ |
1 | %{ | | 1 | %{ |
2 | /* $NetBSD: cgram.y,v 1.315 2021/07/11 20:07:41 rillig Exp $ */ | | 2 | /* $NetBSD: cgram.y,v 1.316 2021/07/11 20:25:54 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.315 2021/07/11 20:07:41 rillig Exp $"); | | 38 | __RCSID("$NetBSD: cgram.y,v 1.316 2021/07/11 20:25:54 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 | |
47 | extern char *yytext; | | 47 | extern 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,27 +271,30 @@ anonymize(sym_t *s) | | | @@ -271,27 +271,30 @@ anonymize(sym_t *s) |
271 | %left T_EQUALITY | | 271 | %left T_EQUALITY |
272 | %left T_RELATIONAL | | 272 | %left T_RELATIONAL |
273 | %left T_SHIFT | | 273 | %left T_SHIFT |
274 | %left T_ADDITIVE | | 274 | %left T_ADDITIVE |
275 | %left T_ASTERISK T_MULTIPLICATIVE | | 275 | %left T_ASTERISK T_MULTIPLICATIVE |
276 | | | 276 | |
277 | %token <y_name> T_NAME | | 277 | %token <y_name> T_NAME |
278 | %token <y_name> T_TYPENAME | | 278 | %token <y_name> T_TYPENAME |
279 | %token <y_val> T_CON | | 279 | %token <y_val> T_CON |
280 | %token <y_string> T_STRING | | 280 | %token <y_string> T_STRING |
281 | | | 281 | |
282 | %type <y_tnode> primary_expression | | 282 | %type <y_tnode> primary_expression |
283 | %type <y_tnode> postfix_expression | | 283 | %type <y_tnode> postfix_expression |
| | | 284 | %type <y_tnode> argument_expression_list |
284 | %type <y_tnode> unary_expression | | 285 | %type <y_tnode> unary_expression |
| | | 286 | %type <y_tnode> cast_expression |
| | | 287 | %type <y_tnode> expr |
285 | | | 288 | |
286 | %type <y_sym> func_decl | | 289 | %type <y_sym> func_decl |
287 | %type <y_sym> notype_decl | | 290 | %type <y_sym> notype_decl |
288 | %type <y_sym> type_decl | | 291 | %type <y_sym> type_decl |
289 | %type <y_type> type_specifier | | 292 | %type <y_type> type_specifier |
290 | %type <y_type> begin_type_typespec | | 293 | %type <y_type> begin_type_typespec |
291 | %type <y_type> notype_type_specifier | | 294 | %type <y_type> notype_type_specifier |
292 | %type <y_type> struct_or_union_specifier | | 295 | %type <y_type> struct_or_union_specifier |
293 | %type <y_type> enum_specifier | | 296 | %type <y_type> enum_specifier |
294 | %type <y_tspec> struct_or_union | | 297 | %type <y_tspec> struct_or_union |
295 | %type <y_sym> braced_struct_declaration_list | | 298 | %type <y_sym> braced_struct_declaration_list |
296 | %type <y_sym> identifier_sym | | 299 | %type <y_sym> identifier_sym |
297 | %type <y_name> identifier | | 300 | %type <y_name> identifier |
| @@ -317,33 +320,30 @@ anonymize(sym_t *s) | | | @@ -317,33 +320,30 @@ anonymize(sym_t *s) |
317 | %type <y_sym> abstract_decl_param_list | | 320 | %type <y_sym> abstract_decl_param_list |
318 | %type <y_sym> direct_param_decl | | 321 | %type <y_sym> direct_param_decl |
319 | %type <y_sym> notype_param_decl | | 322 | %type <y_sym> notype_param_decl |
320 | %type <y_sym> direct_notype_param_decl | | 323 | %type <y_sym> direct_notype_param_decl |
321 | %type <y_qual_ptr> type_qualifier_list_opt | | 324 | %type <y_qual_ptr> type_qualifier_list_opt |
322 | %type <y_qual_ptr> type_qualifier_list | | 325 | %type <y_qual_ptr> type_qualifier_list |
323 | %type <y_qual_ptr> type_qualifier | | 326 | %type <y_qual_ptr> type_qualifier |
324 | %type <y_sym> identifier_list | | 327 | %type <y_sym> identifier_list |
325 | %type <y_sym> abstract_declarator | | 328 | %type <y_sym> abstract_declarator |
326 | %type <y_sym> direct_abstract_declarator | | 329 | %type <y_sym> direct_abstract_declarator |
327 | %type <y_sym> vararg_parameter_type_list | | 330 | %type <y_sym> vararg_parameter_type_list |
328 | %type <y_sym> parameter_type_list | | 331 | %type <y_sym> parameter_type_list |
329 | %type <y_sym> parameter_declaration | | 332 | %type <y_sym> parameter_declaration |
330 | %type <y_tnode> expr | | | |
331 | %type <y_tnode> assignment_expression | | 333 | %type <y_tnode> assignment_expression |
332 | %type <y_tnode> gcc_statement_expr_list | | 334 | %type <y_tnode> gcc_statement_expr_list |
333 | %type <y_tnode> gcc_statement_expr_item | | 335 | %type <y_tnode> gcc_statement_expr_item |
334 | %type <y_tnode> term | | | |
335 | %type <y_tnode> generic_selection | | 336 | %type <y_tnode> generic_selection |
336 | %type <y_tnode> argument_expression_list | | | |
337 | %type <y_op> point_or_arrow | | 337 | %type <y_op> point_or_arrow |
338 | %type <y_type> type_name | | 338 | %type <y_type> type_name |
339 | %type <y_sym> abstract_declaration | | 339 | %type <y_sym> abstract_declaration |
340 | %type <y_tnode> do_while_expr | | 340 | %type <y_tnode> do_while_expr |
341 | %type <y_tnode> expr_opt | | 341 | %type <y_tnode> expr_opt |
342 | %type <y_string> string | | 342 | %type <y_string> string |
343 | %type <y_string> string2 | | 343 | %type <y_string> string2 |
344 | %type <y_name> asm_or_symbolrename_opt | | 344 | %type <y_name> asm_or_symbolrename_opt |
345 | %type <y_range> range | | 345 | %type <y_range> range |
346 | %type <y_seen_statement> block_item_list | | 346 | %type <y_seen_statement> block_item_list |
347 | %type <y_seen_statement> block_item | | 347 | %type <y_seen_statement> block_item |
348 | %type <y_generic> generic_assoc_list | | 348 | %type <y_generic> generic_assoc_list |
349 | %type <y_generic> generic_association | | 349 | %type <y_generic> generic_association |
| @@ -621,27 +621,27 @@ type_attribute: /* See C11 6.7 declara | | | @@ -621,27 +621,27 @@ type_attribute: /* See C11 6.7 declara |
621 | ; | | 621 | ; |
622 | | | 622 | |
623 | type_specifier: /* C99 6.7.2 */ | | 623 | type_specifier: /* C99 6.7.2 */ |
624 | notype_type_specifier | | 624 | notype_type_specifier |
625 | | T_TYPENAME { | | 625 | | T_TYPENAME { |
626 | $$ = getsym($1)->s_type; | | 626 | $$ = getsym($1)->s_type; |
627 | } | | 627 | } |
628 | ; | | 628 | ; |
629 | | | 629 | |
630 | notype_type_specifier: | | 630 | notype_type_specifier: |
631 | T_TYPE { | | 631 | T_TYPE { |
632 | $$ = gettyp($1); | | 632 | $$ = gettyp($1); |
633 | } | | 633 | } |
634 | | T_TYPEOF term { | | 634 | | T_TYPEOF cast_expression { /* GCC extension */ |
635 | $$ = $2->tn_type; | | 635 | $$ = $2->tn_type; |
636 | } | | 636 | } |
637 | | struct_or_union_specifier { | | 637 | | struct_or_union_specifier { |
638 | end_declaration_level(); | | 638 | end_declaration_level(); |
639 | $$ = $1; | | 639 | $$ = $1; |
640 | } | | 640 | } |
641 | | enum_specifier { | | 641 | | enum_specifier { |
642 | end_declaration_level(); | | 642 | end_declaration_level(); |
643 | $$ = $1; | | 643 | $$ = $1; |
644 | } | | 644 | } |
645 | ; | | 645 | ; |
646 | | | 646 | |
647 | struct_or_union_specifier: /* C99 6.7.2.1 */ | | 647 | struct_or_union_specifier: /* C99 6.7.2.1 */ |
| @@ -1343,27 +1343,27 @@ abstract_declaration: | | | @@ -1343,27 +1343,27 @@ abstract_declaration: |
1343 | | begin_type_noclass_declspecs end_type abstract_declarator { | | 1343 | | begin_type_noclass_declspecs end_type abstract_declarator { |
1344 | $$ = declare_1_abstract($3); | | 1344 | $$ = declare_1_abstract($3); |
1345 | } | | 1345 | } |
1346 | ; | | 1346 | ; |
1347 | | | 1347 | |
1348 | abstract_declarator: /* C99 6.7.6 */ | | 1348 | abstract_declarator: /* C99 6.7.6 */ |
1349 | pointer { | | 1349 | pointer { |
1350 | $$ = add_pointer(abstract_name(), $1); | | 1350 | $$ = add_pointer(abstract_name(), $1); |
1351 | } | | 1351 | } |
1352 | | direct_abstract_declarator | | 1352 | | direct_abstract_declarator |
1353 | | pointer direct_abstract_declarator { | | 1353 | | pointer direct_abstract_declarator { |
1354 | $$ = add_pointer($2, $1); | | 1354 | $$ = add_pointer($2, $1); |
1355 | } | | 1355 | } |
1356 | | T_TYPEOF term { /* GCC extension */ | | 1356 | | T_TYPEOF cast_expression { /* GCC extension */ |
1357 | $$ = mktempsym($2->tn_type); | | 1357 | $$ = mktempsym($2->tn_type); |
1358 | } | | 1358 | } |
1359 | ; | | 1359 | ; |
1360 | | | 1360 | |
1361 | direct_abstract_declarator: /* C99 6.7.6 */ | | 1361 | direct_abstract_declarator: /* C99 6.7.6 */ |
1362 | T_LPAREN abstract_declarator T_RPAREN { | | 1362 | T_LPAREN abstract_declarator T_RPAREN { |
1363 | $$ = $2; | | 1363 | $$ = $2; |
1364 | } | | 1364 | } |
1365 | | T_LBRACK T_RBRACK { | | 1365 | | T_LBRACK T_RBRACK { |
1366 | $$ = add_array(abstract_name(), false, 0); | | 1366 | $$ = add_array(abstract_name(), false, 0); |
1367 | } | | 1367 | } |
1368 | | T_LBRACK array_size T_RBRACK { | | 1368 | | T_LBRACK array_size T_RBRACK { |
1369 | $$ = add_array(abstract_name(), true, to_int_constant($2, false)); | | 1369 | $$ = add_array(abstract_name(), true, to_int_constant($2, false)); |
| @@ -1711,28 +1711,28 @@ expr: /* C99 6.5 */ | | | @@ -1711,28 +1711,28 @@ expr: /* C99 6.5 */ |
1711 | } | | 1711 | } |
1712 | | expr T_QUEST expr T_COLON expr { | | 1712 | | expr T_QUEST expr T_COLON expr { |
1713 | $$ = build(QUEST, $1, build(COLON, $3, $5)); | | 1713 | $$ = build(QUEST, $1, build(COLON, $3, $5)); |
1714 | } | | 1714 | } |
1715 | | expr T_ASSIGN expr { | | 1715 | | expr T_ASSIGN expr { |
1716 | $$ = build(ASSIGN, $1, $3); | | 1716 | $$ = build(ASSIGN, $1, $3); |
1717 | } | | 1717 | } |
1718 | | expr T_OPASSIGN expr { | | 1718 | | expr T_OPASSIGN expr { |
1719 | $$ = build($2, $1, $3); | | 1719 | $$ = build($2, $1, $3); |
1720 | } | | 1720 | } |
1721 | | expr T_COMMA expr { | | 1721 | | expr T_COMMA expr { |
1722 | $$ = build(COMMA, $1, $3); | | 1722 | $$ = build(COMMA, $1, $3); |
1723 | } | | 1723 | } |
1724 | | term | | 1724 | | cast_expression |
1725 | | generic_selection | | 1725 | | generic_selection /* TODO: move to primary_expression */ |
1726 | ; | | 1726 | ; |
1727 | | | 1727 | |
1728 | assignment_expression: /* C99 6.5.16 */ | | 1728 | assignment_expression: /* C99 6.5.16 */ |
1729 | expr %prec T_ASSIGN | | 1729 | expr %prec T_ASSIGN |
1730 | ; | | 1730 | ; |
1731 | | | 1731 | |
1732 | primary_expression: /* C99 6.5.1 */ | | 1732 | primary_expression: /* C99 6.5.1 */ |
1733 | T_NAME { | | 1733 | T_NAME { |
1734 | /* XXX really necessary? */ | | 1734 | /* XXX really necessary? */ |
1735 | if (yychar < 0) | | 1735 | if (yychar < 0) |
1736 | yychar = yylex(); | | 1736 | yychar = yylex(); |
1737 | $$ = new_name_node(getsym($1), yychar); | | 1737 | $$ = new_name_node(getsym($1), yychar); |
1738 | } | | 1738 | } |
| @@ -1808,70 +1808,70 @@ postfix_expression: /* C99 6.5.2 */ | | | @@ -1808,70 +1808,70 @@ postfix_expression: /* C99 6.5.2 */ |
1808 | /* ({ }) is a GCC extension */ | | 1808 | /* ({ }) is a GCC extension */ |
1809 | gnuism(320); | | 1809 | gnuism(320); |
1810 | } compound_statement_rbrace T_RPAREN { | | 1810 | } compound_statement_rbrace T_RPAREN { |
1811 | $$ = new_name_node(*current_initsym(), 0); | | 1811 | $$ = new_name_node(*current_initsym(), 0); |
1812 | end_initialization(); | | 1812 | end_initialization(); |
1813 | } | | 1813 | } |
1814 | ; | | 1814 | ; |
1815 | | | 1815 | |
1816 | unary_expression: /* C99 6.5.3 */ | | 1816 | unary_expression: /* C99 6.5.3 */ |
1817 | postfix_expression | | 1817 | postfix_expression |
1818 | | T_INCDEC unary_expression { | | 1818 | | T_INCDEC unary_expression { |
1819 | $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL); | | 1819 | $$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL); |
1820 | } | | 1820 | } |
1821 | | T_AMPER term { | | 1821 | | T_AMPER cast_expression { |
1822 | $$ = build(ADDR, $2, NULL); | | 1822 | $$ = build(ADDR, $2, NULL); |
1823 | } | | 1823 | } |
1824 | | T_ASTERISK term { | | 1824 | | T_ASTERISK cast_expression { |
1825 | $$ = build(INDIR, $2, NULL); | | 1825 | $$ = build(INDIR, $2, NULL); |
1826 | } | | 1826 | } |
1827 | | T_ADDITIVE term { | | 1827 | | T_ADDITIVE cast_expression { |
1828 | if (tflag && $1 == PLUS) { | | 1828 | if (tflag && $1 == PLUS) { |
1829 | /* unary + is illegal in traditional C */ | | 1829 | /* unary + is illegal in traditional C */ |
1830 | warning(100); | | 1830 | warning(100); |
1831 | } | | 1831 | } |
1832 | $$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL); | | 1832 | $$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL); |
1833 | } | | 1833 | } |
1834 | | T_COMPLEMENT term { | | 1834 | | T_COMPLEMENT cast_expression { |
1835 | $$ = build(COMPL, $2, NULL); | | 1835 | $$ = build(COMPL, $2, NULL); |
1836 | } | | 1836 | } |
1837 | | T_LOGNOT term { | | 1837 | | T_LOGNOT cast_expression { |
1838 | $$ = build(NOT, $2, NULL); | | 1838 | $$ = build(NOT, $2, NULL); |
1839 | } | | 1839 | } |
1840 | | T_REAL term { /* GCC c_parser_unary_expression */ | | 1840 | | T_REAL cast_expression { /* GCC c_parser_unary_expression */ |
1841 | $$ = build(REAL, $2, NULL); | | 1841 | $$ = build(REAL, $2, NULL); |
1842 | } | | 1842 | } |
1843 | | T_IMAG term { /* GCC c_parser_unary_expression */ | | 1843 | | T_IMAG cast_expression { /* GCC c_parser_unary_expression */ |
1844 | $$ = build(IMAG, $2, NULL); | | 1844 | $$ = build(IMAG, $2, NULL); |
1845 | } | | 1845 | } |
1846 | | T_EXTENSION term { /* GCC c_parser_unary_expression */ | | 1846 | | T_EXTENSION cast_expression { /* GCC c_parser_unary_expression */ |
1847 | $$ = $2; | | 1847 | $$ = $2; |
1848 | } | | 1848 | } |
1849 | | T_SIZEOF unary_expression { | | 1849 | | T_SIZEOF unary_expression { |
1850 | $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type); | | 1850 | $$ = $2 == NULL ? NULL : build_sizeof($2->tn_type); |
1851 | if ($$ != NULL) | | 1851 | if ($$ != NULL) |
1852 | check_expr_misc($2, false, false, false, false, false, true); | | 1852 | check_expr_misc($2, false, false, false, false, false, true); |
1853 | } | | 1853 | } |
1854 | | T_SIZEOF T_LPAREN type_name T_RPAREN { | | 1854 | | T_SIZEOF T_LPAREN type_name T_RPAREN { |
1855 | $$ = build_sizeof($3); | | 1855 | $$ = build_sizeof($3); |
1856 | } | | 1856 | } |
1857 | | T_ALIGNOF T_LPAREN type_name T_RPAREN { /* C11 6.5.3 */ | | 1857 | | T_ALIGNOF T_LPAREN type_name T_RPAREN { /* C11 6.5.3 */ |
1858 | $$ = build_alignof($3); | | 1858 | $$ = build_alignof($3); |
1859 | } | | 1859 | } |
1860 | ; | | 1860 | ; |
1861 | | | 1861 | |
1862 | term: /* see C99 6.5.1 */ | | 1862 | cast_expression: /* see C99 6.5.1 */ |
1863 | unary_expression | | 1863 | unary_expression |
1864 | | T_LPAREN type_name T_RPAREN term { | | 1864 | | T_LPAREN type_name T_RPAREN cast_expression { |
1865 | $$ = cast($4, $2); | | 1865 | $$ = cast($4, $2); |
1866 | } | | 1866 | } |
1867 | ; | | 1867 | ; |
1868 | | | 1868 | |
1869 | generic_selection: /* C11 6.5.1.1 */ | | 1869 | generic_selection: /* C11 6.5.1.1 */ |
1870 | T_GENERIC T_LPAREN assignment_expression T_COMMA | | 1870 | T_GENERIC T_LPAREN assignment_expression T_COMMA |
1871 | generic_assoc_list T_RPAREN { | | 1871 | generic_assoc_list T_RPAREN { |
1872 | /* generic selection requires C11 or later */ | | 1872 | /* generic selection requires C11 or later */ |
1873 | c11ism(345); | | 1873 | c11ism(345); |
1874 | $$ = build_generic_selection($3, $5); | | 1874 | $$ = build_generic_selection($3, $5); |
1875 | } | | 1875 | } |
1876 | ; | | 1876 | ; |
1877 | | | 1877 | |