| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: decl.c,v 1.205 2021/07/15 23:42:49 rillig Exp $ */ | | 1 | /* $NetBSD: decl.c,v 1.206 2021/07/15 23:47:00 rillig Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. | | 4 | * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. |
5 | * Copyright (c) 1994, 1995 Jochen Pohl | | 5 | * Copyright (c) 1994, 1995 Jochen Pohl |
6 | * All Rights Reserved. | | 6 | * All Rights Reserved. |
7 | * | | 7 | * |
8 | * Redistribution and use in source and binary forms, with or without | | 8 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions | | 9 | * modification, are permitted provided that the following conditions |
10 | * are met: | | 10 | * are met: |
11 | * 1. Redistributions of source code must retain the above copyright | | 11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. | | 12 | * notice, this list of conditions and the following disclaimer. |
13 | * 2. Redistributions in binary form must reproduce the above copyright | | 13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the | | 14 | * notice, this list of conditions and the following disclaimer in the |
| @@ -28,53 +28,52 @@ | | | @@ -28,53 +28,52 @@ |
28 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | | 28 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | | 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | | 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | | 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
32 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | | 32 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
33 | */ | | 33 | */ |
34 | | | 34 | |
35 | #if HAVE_NBTOOL_CONFIG_H | | 35 | #if HAVE_NBTOOL_CONFIG_H |
36 | #include "nbtool_config.h" | | 36 | #include "nbtool_config.h" |
37 | #endif | | 37 | #endif |
38 | | | 38 | |
39 | #include <sys/cdefs.h> | | 39 | #include <sys/cdefs.h> |
40 | #if defined(__RCSID) && !defined(lint) | | 40 | #if defined(__RCSID) && !defined(lint) |
41 | __RCSID("$NetBSD: decl.c,v 1.205 2021/07/15 23:42:49 rillig Exp $"); | | 41 | __RCSID("$NetBSD: decl.c,v 1.206 2021/07/15 23:47:00 rillig Exp $"); |
42 | #endif | | 42 | #endif |
43 | | | 43 | |
44 | #include <sys/param.h> | | 44 | #include <sys/param.h> |
45 | #include <limits.h> | | 45 | #include <limits.h> |
46 | #include <stdlib.h> | | 46 | #include <stdlib.h> |
47 | #include <string.h> | | 47 | #include <string.h> |
48 | | | 48 | |
49 | #include "lint1.h" | | 49 | #include "lint1.h" |
50 | | | 50 | |
51 | const char *unnamed = "<unnamed>"; | | 51 | const char *unnamed = "<unnamed>"; |
52 | | | 52 | |
53 | /* shared type structures for arithmetic types and void */ | | 53 | /* shared type structures for arithmetic types and void */ |
54 | static type_t *typetab; | | 54 | static type_t *typetab; |
55 | | | 55 | |
56 | /* value of next enumerator during declaration of enum types */ | | 56 | /* value of next enumerator during declaration of enum types */ |
57 | int enumval; | | 57 | int enumval; |
58 | | | 58 | |
59 | /* | | 59 | /* |
60 | * pointer to top element of a stack which contains information local | | 60 | * pointer to top element of a stack which contains information local |
61 | * to nested declarations | | 61 | * to nested declarations |
62 | */ | | 62 | */ |
63 | dinfo_t *dcs; | | 63 | dinfo_t *dcs; |
64 | | | 64 | |
65 | static type_t *tdeferr(type_t *, tspec_t); | | 65 | static type_t *tdeferr(type_t *, tspec_t); |
66 | static void settdsym(type_t *, sym_t *); | | 66 | static void settdsym(type_t *, sym_t *); |
67 | static tspec_t merge_type_specifiers(tspec_t, tspec_t); | | | |
68 | static void align(int, int); | | 67 | static void align(int, int); |
69 | static sym_t *newtag(sym_t *, scl_t, bool, bool); | | 68 | static sym_t *newtag(sym_t *, scl_t, bool, bool); |
70 | static bool eqargs(const type_t *, const type_t *, bool *); | | 69 | static bool eqargs(const type_t *, const type_t *, bool *); |
71 | static bool mnoarg(const type_t *, bool *); | | 70 | static bool mnoarg(const type_t *, bool *); |
72 | static bool check_old_style_definition(sym_t *, sym_t *); | | 71 | static bool check_old_style_definition(sym_t *, sym_t *); |
73 | static bool check_prototype_declaration(sym_t *, sym_t *); | | 72 | static bool check_prototype_declaration(sym_t *, sym_t *); |
74 | static sym_t *new_style_function(sym_t *, sym_t *); | | 73 | static sym_t *new_style_function(sym_t *, sym_t *); |
75 | static void old_style_function(sym_t *, sym_t *); | | 74 | static void old_style_function(sym_t *, sym_t *); |
76 | static void declare_external_in_block(sym_t *); | | 75 | static void declare_external_in_block(sym_t *); |
77 | static bool check_init(sym_t *); | | 76 | static bool check_init(sym_t *); |
78 | static void check_argument_usage(bool, sym_t *); | | 77 | static void check_argument_usage(bool, sym_t *); |
79 | static void check_variable_usage(bool, sym_t *); | | 78 | static void check_variable_usage(bool, sym_t *); |
80 | static void check_label_usage(sym_t *); | | 79 | static void check_label_usage(sym_t *); |
| @@ -372,46 +371,67 @@ add_type(type_t *tp) | | | @@ -372,46 +371,67 @@ add_type(type_t *tp) |
372 | dcs->d_type = tp; | | 371 | dcs->d_type = tp; |
373 | } else { | | 372 | } else { |
374 | /* | | 373 | /* |
375 | * remember specifiers "void", "char", "int", | | 374 | * remember specifiers "void", "char", "int", |
376 | * or "_Complex" in dcs->d_abstract_type | | 375 | * or "_Complex" in dcs->d_abstract_type |
377 | */ | | 376 | */ |
378 | if (dcs->d_abstract_type != NOTSPEC) | | 377 | if (dcs->d_abstract_type != NOTSPEC) |
379 | /* more than one, print error in end_type() */ | | 378 | /* more than one, print error in end_type() */ |
380 | dcs->d_terr = true; | | 379 | dcs->d_terr = true; |
381 | dcs->d_abstract_type = t; | | 380 | dcs->d_abstract_type = t; |
382 | } | | 381 | } |
383 | } | | 382 | } |
384 | | | 383 | |
| | | 384 | /* Merge the signedness into the abstract type. */ |
| | | 385 | static tspec_t |
| | | 386 | merge_signedness(tspec_t t, tspec_t s) |
| | | 387 | { |
| | | 388 | |
| | | 389 | if (s != SIGNED && s != UNSIGN) |
| | | 390 | return t; |
| | | 391 | |
| | | 392 | if (t == CHAR) |
| | | 393 | return s == SIGNED ? SCHAR : UCHAR; |
| | | 394 | if (t == SHORT) |
| | | 395 | return s == SIGNED ? SHORT : USHORT; |
| | | 396 | if (t == INT) |
| | | 397 | return s == SIGNED ? INT : UINT; |
| | | 398 | if (t == LONG) |
| | | 399 | return s == SIGNED ? LONG : ULONG; |
| | | 400 | if (t == QUAD) |
| | | 401 | return s == SIGNED ? QUAD : UQUAD; |
| | | 402 | return t; |
| | | 403 | } |
| | | 404 | |
385 | /* | | 405 | /* |
386 | * called if a list of declaration specifiers contains a typedef name | | 406 | * called if a list of declaration specifiers contains a typedef name |
387 | * and other specifiers (except struct, union, enum, typedef name) | | 407 | * and other specifiers (except struct, union, enum, typedef name) |
388 | */ | | 408 | */ |
389 | static type_t * | | 409 | static type_t * |
390 | tdeferr(type_t *td, tspec_t t) | | 410 | tdeferr(type_t *td, tspec_t t) |
391 | { | | 411 | { |
392 | tspec_t t2; | | 412 | tspec_t t2; |
393 | | | 413 | |
394 | t2 = td->t_tspec; | | 414 | t2 = td->t_tspec; |
395 | | | 415 | |
396 | switch (t) { | | 416 | switch (t) { |
397 | case SIGNED: | | 417 | case SIGNED: |
398 | case UNSIGN: | | 418 | case UNSIGN: |
399 | if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG || | | 419 | if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG || |
400 | t2 == QUAD) { | | 420 | t2 == QUAD) { |
401 | if (!tflag) | | 421 | if (!tflag) |
402 | /* modifying typedef with '%s'; only ... */ | | 422 | /* modifying typedef with '%s'; only ... */ |
403 | warning(5, ttab[t].tt_name); | | 423 | warning(5, ttab[t].tt_name); |
404 | td = dup_type(gettyp(merge_type_specifiers(t2, t))); | | 424 | td = dup_type(gettyp(merge_signedness(t2, t))); |
405 | td->t_typedef = true; | | 425 | td->t_typedef = true; |
406 | return td; | | 426 | return td; |
407 | } | | 427 | } |
408 | break; | | 428 | break; |
409 | case SHORT: | | 429 | case SHORT: |
410 | if (t2 == INT || t2 == UINT) { | | 430 | if (t2 == INT || t2 == UINT) { |
411 | /* modifying typedef with '%s'; only qualifiers ... */ | | 431 | /* modifying typedef with '%s'; only qualifiers ... */ |
412 | warning(5, "short"); | | 432 | warning(5, "short"); |
413 | td = dup_type(gettyp(t2 == INT ? SHORT : USHORT)); | | 433 | td = dup_type(gettyp(t2 == INT ? SHORT : USHORT)); |
414 | td->t_typedef = true; | | 434 | td->t_typedef = true; |
415 | return td; | | 435 | return td; |
416 | } | | 436 | } |
417 | break; | | 437 | break; |
| @@ -804,27 +824,27 @@ dcs_merge_declaration_specifiers(void) | | | @@ -804,27 +824,27 @@ dcs_merge_declaration_specifiers(void) |
804 | warning(266); | | 824 | warning(266); |
805 | } | | 825 | } |
806 | if (l == LONG && t == DCOMPLEX) { | | 826 | if (l == LONG && t == DCOMPLEX) { |
807 | l = NOTSPEC; | | 827 | l = NOTSPEC; |
808 | t = LCOMPLEX; | | 828 | t = LCOMPLEX; |
809 | } | | 829 | } |
810 | | | 830 | |
811 | if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) { | | 831 | if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) { |
812 | dcs->d_terr = true; | | 832 | dcs->d_terr = true; |
813 | l = s = NOTSPEC; | | 833 | l = s = NOTSPEC; |
814 | } | | 834 | } |
815 | if (l != NOTSPEC) | | 835 | if (l != NOTSPEC) |
816 | t = l; | | 836 | t = l; |
817 | dcs->d_type = gettyp(merge_type_specifiers(t, s)); | | 837 | dcs->d_type = gettyp(merge_signedness(t, s)); |
818 | } | | 838 | } |
819 | | | 839 | |
820 | /* | | 840 | /* |
821 | * Create a type structure from the information gathered in | | 841 | * Create a type structure from the information gathered in |
822 | * the declaration stack. | | 842 | * the declaration stack. |
823 | * Complain about storage classes which are not possible in current | | 843 | * Complain about storage classes which are not possible in current |
824 | * context. | | 844 | * context. |
825 | */ | | 845 | */ |
826 | void | | 846 | void |
827 | end_type(void) | | 847 | end_type(void) |
828 | { | | 848 | { |
829 | | | 849 | |
830 | dcs_merge_declaration_specifiers(); | | 850 | dcs_merge_declaration_specifiers(); |
| @@ -849,49 +869,26 @@ end_type(void) | | | @@ -849,49 +869,26 @@ end_type(void) |
849 | lint_assert(dcs->d_type->t_typedef); | | 869 | lint_assert(dcs->d_type->t_typedef); |
850 | /* typedef already qualified with '%s' */ | | 870 | /* typedef already qualified with '%s' */ |
851 | warning(68, "volatile"); | | 871 | warning(68, "volatile"); |
852 | } | | 872 | } |
853 | | | 873 | |
854 | if (dcs->d_const || dcs->d_volatile) { | | 874 | if (dcs->d_const || dcs->d_volatile) { |
855 | dcs->d_type = dup_type(dcs->d_type); | | 875 | dcs->d_type = dup_type(dcs->d_type); |
856 | dcs->d_type->t_const |= dcs->d_const; | | 876 | dcs->d_type->t_const |= dcs->d_const; |
857 | dcs->d_type->t_volatile |= dcs->d_volatile; | | 877 | dcs->d_type->t_volatile |= dcs->d_volatile; |
858 | } | | 878 | } |
859 | } | | 879 | } |
860 | | | 880 | |
861 | /* | | 881 | /* |
862 | * Merge type specifiers (char, ..., long long, signed, unsigned). | | | |
863 | */ | | | |
864 | static tspec_t | | | |
865 | merge_type_specifiers(tspec_t t, tspec_t s) | | | |
866 | { | | | |
867 | | | | |
868 | if (s != SIGNED && s != UNSIGN) | | | |
869 | return t; | | | |
870 | | | | |
871 | if (t == CHAR) | | | |
872 | return s == SIGNED ? SCHAR : UCHAR; | | | |
873 | if (t == SHORT) | | | |
874 | return s == SIGNED ? SHORT : USHORT; | | | |
875 | if (t == INT) | | | |
876 | return s == SIGNED ? INT : UINT; | | | |
877 | if (t == LONG) | | | |
878 | return s == SIGNED ? LONG : ULONG; | | | |
879 | if (t == QUAD) | | | |
880 | return s == SIGNED ? QUAD : UQUAD; | | | |
881 | return t; | | | |
882 | } | | | |
883 | | | | |
884 | /* | | | |
885 | * Return the length of a type in bits. | | 882 | * Return the length of a type in bits. |
886 | * | | 883 | * |
887 | * Printing a message if the outermost dimension of an array is 0 must | | 884 | * Printing a message if the outermost dimension of an array is 0 must |
888 | * be done by the caller. All other problems are reported by length() | | 885 | * be done by the caller. All other problems are reported by length() |
889 | * if name is not NULL. | | 886 | * if name is not NULL. |
890 | */ | | 887 | */ |
891 | int | | 888 | int |
892 | length(const type_t *tp, const char *name) | | 889 | length(const type_t *tp, const char *name) |
893 | { | | 890 | { |
894 | int elem, elsz; | | 891 | int elem, elsz; |
895 | | | 892 | |
896 | elem = 1; | | 893 | elem = 1; |
897 | while (tp != NULL && tp->t_tspec == ARRAY) { | | 894 | while (tp != NULL && tp->t_tspec == ARRAY) { |