Annotation of src/usr.bin/xlint/lint1/decl.c, Revision 1.173
1.173 ! rillig 1: /* $NetBSD: decl.c,v 1.172 2021/04/10 18:06:53 rillig Exp $ */
1.2 cgd 2:
1.1 cgd 3: /*
1.12 cgd 4: * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
1.1 cgd 5: * Copyright (c) 1994, 1995 Jochen Pohl
6: * All Rights Reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by Jochen Pohl for
19: * The NetBSD Project.
20: * 4. The name of the author may not be used to endorse or promote products
21: * derived from this software without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
32: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33: */
34:
1.33 jmc 35: #if HAVE_NBTOOL_CONFIG_H
36: #include "nbtool_config.h"
37: #endif
38:
1.13 christos 39: #include <sys/cdefs.h>
1.30 tv 40: #if defined(__RCSID) && !defined(lint)
1.173 ! rillig 41: __RCSID("$NetBSD: decl.c,v 1.172 2021/04/10 18:06:53 rillig Exp $");
1.1 cgd 42: #endif
43:
44: #include <sys/param.h>
45: #include <limits.h>
46: #include <stdlib.h>
47: #include <string.h>
48:
49: #include "lint1.h"
50:
51: const char *unnamed = "<unnamed>";
52:
1.111 rillig 53: /* shared type structures for arithmetic types and void */
1.1 cgd 54: static type_t *typetab;
55:
56: /* value of next enumerator during declaration of enum types */
57: int enumval;
58:
59: /*
1.73 rillig 60: * pointer to top element of a stack which contains information local
1.1 cgd 61: * to nested declarations
62: */
63: dinfo_t *dcs;
64:
1.22 lukem 65: static type_t *tdeferr(type_t *, tspec_t);
66: static void settdsym(type_t *, sym_t *);
1.77 rillig 67: static tspec_t merge_type_specifiers(tspec_t, tspec_t);
1.22 lukem 68: static void align(int, int);
1.122 rillig 69: static sym_t *newtag(sym_t *, scl_t, bool, bool);
70: static bool eqargs(const type_t *, const type_t *, bool *);
71: static bool mnoarg(const type_t *, bool *);
72: static bool check_old_style_definition(sym_t *, sym_t *);
73: static bool check_prototype_declaration(sym_t *, sym_t *);
1.75 rillig 74: static sym_t *new_style_function(sym_t *, sym_t *);
75: static void old_style_function(sym_t *, sym_t *);
1.116 rillig 76: static void declare_external_in_block(sym_t *);
1.122 rillig 77: static bool check_init(sym_t *);
78: static void check_argument_usage(bool, sym_t *);
79: static void check_variable_usage(bool, sym_t *);
1.75 rillig 80: static void check_label_usage(sym_t *);
81: static void check_tag_usage(sym_t *);
1.120 rillig 82: static void check_global_variable(const sym_t *);
83: static void check_global_variable_size(const sym_t *);
1.1 cgd 84:
85: /*
86: * initializes all global vars used in declarations
87: */
88: void
1.22 lukem 89: initdecl(void)
1.1 cgd 90: {
1.29 thorpej 91: int i;
1.1 cgd 92:
93: /* declaration stack */
1.170 rillig 94: dcs = xcalloc(1, sizeof(*dcs));
1.3 jpo 95: dcs->d_ctx = EXTERN;
96: dcs->d_ldlsym = &dcs->d_dlsyms;
1.1 cgd 97:
98: /* type information and classification */
1.29 thorpej 99: inittyp();
1.22 lukem 100:
1.1 cgd 101: /* shared type structures */
1.170 rillig 102: typetab = xcalloc(NTSPEC, sizeof(*typetab));
1.1 cgd 103: for (i = 0; i < NTSPEC; i++)
104: typetab[i].t_tspec = NOTSPEC;
1.34 yamt 105: typetab[BOOL].t_tspec = BOOL;
1.1 cgd 106: typetab[CHAR].t_tspec = CHAR;
107: typetab[SCHAR].t_tspec = SCHAR;
108: typetab[UCHAR].t_tspec = UCHAR;
109: typetab[SHORT].t_tspec = SHORT;
110: typetab[USHORT].t_tspec = USHORT;
111: typetab[INT].t_tspec = INT;
112: typetab[UINT].t_tspec = UINT;
113: typetab[LONG].t_tspec = LONG;
114: typetab[ULONG].t_tspec = ULONG;
115: typetab[QUAD].t_tspec = QUAD;
116: typetab[UQUAD].t_tspec = UQUAD;
117: typetab[FLOAT].t_tspec = FLOAT;
118: typetab[DOUBLE].t_tspec = DOUBLE;
119: typetab[LDOUBLE].t_tspec = LDOUBLE;
1.39 christos 120: typetab[FCOMPLEX].t_tspec = FCOMPLEX;
121: typetab[DCOMPLEX].t_tspec = DCOMPLEX;
1.42 matt 122: typetab[LCOMPLEX].t_tspec = LCOMPLEX;
1.39 christos 123: typetab[COMPLEX].t_tspec = COMPLEX;
1.1 cgd 124: typetab[VOID].t_tspec = VOID;
125: /*
126: * Next two are not real types. They are only used by the parser
127: * to return keywords "signed" and "unsigned"
128: */
129: typetab[SIGNED].t_tspec = SIGNED;
130: typetab[UNSIGN].t_tspec = UNSIGN;
131: }
132:
133: /*
1.73 rillig 134: * Returns a shared type structure for arithmetic types and void.
1.1 cgd 135: *
1.169 rillig 136: * It's important to duplicate this structure (using dup_type() or
137: * expr_dup_type()) if it is to be modified (adding qualifiers or anything
138: * else).
1.1 cgd 139: */
140: type_t *
1.22 lukem 141: gettyp(tspec_t t)
1.1 cgd 142: {
1.22 lukem 143:
1.172 rillig 144: /* TODO: make the return type 'const' */
1.76 rillig 145: return &typetab[t];
1.1 cgd 146: }
147:
148: type_t *
1.169 rillig 149: dup_type(const type_t *tp)
1.1 cgd 150: {
151: type_t *ntp;
152:
1.163 rillig 153: ntp = getblk(sizeof(*ntp));
1.85 rillig 154: *ntp = *tp;
1.76 rillig 155: return ntp;
1.1 cgd 156: }
157:
158: /*
1.169 rillig 159: * Use expr_dup_type() instead of dup_type() inside expressions (if the
1.1 cgd 160: * allocated memory should be freed after the expr).
161: */
162: type_t *
1.169 rillig 163: expr_dup_type(const type_t *tp)
1.1 cgd 164: {
165: type_t *ntp;
166:
1.170 rillig 167: ntp = expr_zalloc(sizeof(*ntp));
1.85 rillig 168: *ntp = *tp;
1.76 rillig 169: return ntp;
1.1 cgd 170: }
171:
172: /*
1.131 rillig 173: * Returns whether the argument is void or an incomplete array,
1.1 cgd 174: * struct, union or enum type.
175: */
1.122 rillig 176: bool
1.132 rillig 177: is_incomplete(const type_t *tp)
1.1 cgd 178: {
179: tspec_t t;
180:
181: if ((t = tp->t_tspec) == VOID) {
1.124 rillig 182: return true;
1.1 cgd 183: } else if (t == ARRAY) {
1.136 rillig 184: return tp->t_incomplete_array;
1.1 cgd 185: } else if (t == STRUCT || t == UNION) {
1.134 rillig 186: return tp->t_str->sou_incomplete;
1.1 cgd 187: } else if (t == ENUM) {
1.135 rillig 188: return tp->t_enum->en_incomplete;
1.1 cgd 189: }
1.124 rillig 190: return false;
1.1 cgd 191: }
192:
193: /*
1.74 rillig 194: * Mark an array, struct, union or enum type as complete or incomplete.
1.1 cgd 195: */
196: void
1.122 rillig 197: setcomplete(type_t *tp, bool complete)
1.1 cgd 198: {
199: tspec_t t;
200:
201: if ((t = tp->t_tspec) == ARRAY) {
1.136 rillig 202: tp->t_incomplete_array = !complete;
1.1 cgd 203: } else if (t == STRUCT || t == UNION) {
1.134 rillig 204: tp->t_str->sou_incomplete = !complete;
1.1 cgd 205: } else {
1.92 rillig 206: lint_assert(t == ENUM);
1.135 rillig 207: tp->t_enum->en_incomplete = !complete;
1.1 cgd 208: }
209: }
210:
211: /*
1.3 jpo 212: * Remember the storage class of the current declaration in dcs->d_scl
1.1 cgd 213: * (the top element of the declaration stack) and detect multiple
214: * storage classes.
215: */
216: void
1.77 rillig 217: add_storage_class(scl_t sc)
1.1 cgd 218: {
1.22 lukem 219:
1.4 jpo 220: if (sc == INLINE) {
221: if (dcs->d_inline)
222: /* duplicate '%s' */
223: warning(10, "inline");
1.122 rillig 224: dcs->d_inline = true;
1.4 jpo 225: return;
226: }
1.147 rillig 227: if (dcs->d_type != NULL || dcs->d_abstract_type != NOTSPEC ||
228: dcs->d_sign_mod != NOTSPEC || dcs->d_rank_mod != NOTSPEC) {
1.1 cgd 229: /* storage class after type is obsolescent */
230: warning(83);
231: }
1.3 jpo 232: if (dcs->d_scl == NOSCL) {
233: dcs->d_scl = sc;
1.1 cgd 234: } else {
235: /*
236: * multiple storage classes. An error will be reported in
237: * deftyp().
238: */
1.122 rillig 239: dcs->d_mscl = true;
1.1 cgd 240: }
241: }
242:
243: /*
244: * Remember the type, modifier or typedef name returned by the parser
245: * in *dcs (top element of decl stack). This information is used in
246: * deftyp() to build the type used for all declarators in this
247: * declaration.
248: *
1.73 rillig 249: * If tp->t_typedef is 1, the type comes from a previously defined typename.
1.1 cgd 250: * Otherwise it comes from a type specifier (int, long, ...) or a
251: * struct/union/enum tag.
252: */
253: void
1.77 rillig 254: add_type(type_t *tp)
1.1 cgd 255: {
256: tspec_t t;
1.49 christos 257: #ifdef DEBUG
1.98 rillig 258: printf("%s: %s\n", __func__, type_name(tp));
1.49 christos 259: #endif
1.1 cgd 260: if (tp->t_typedef) {
1.92 rillig 261: /*
262: * something like "typedef int a; int a b;"
263: * This should not happen with current grammar.
264: */
265: lint_assert(dcs->d_type == NULL);
1.147 rillig 266: lint_assert(dcs->d_abstract_type == NOTSPEC);
267: lint_assert(dcs->d_sign_mod == NOTSPEC);
268: lint_assert(dcs->d_rank_mod == NOTSPEC);
1.92 rillig 269:
1.3 jpo 270: dcs->d_type = tp;
1.1 cgd 271: return;
272: }
273:
274: t = tp->t_tspec;
275:
276: if (t == STRUCT || t == UNION || t == ENUM) {
277: /*
278: * something like "int struct a ..."
279: * struct/union/enum with anything else is not allowed
280: */
1.147 rillig 281: if (dcs->d_type != NULL || dcs->d_abstract_type != NOTSPEC ||
282: dcs->d_rank_mod != NOTSPEC || dcs->d_sign_mod != NOTSPEC) {
1.1 cgd 283: /*
284: * remember that an error must be reported in
285: * deftyp().
286: */
1.122 rillig 287: dcs->d_terr = true;
1.147 rillig 288: dcs->d_abstract_type = NOTSPEC;
289: dcs->d_sign_mod = NOTSPEC;
290: dcs->d_rank_mod = NOTSPEC;
1.1 cgd 291: }
1.3 jpo 292: dcs->d_type = tp;
1.1 cgd 293: return;
294: }
295:
1.3 jpo 296: if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
1.1 cgd 297: /*
298: * something like "struct a int"
299: * struct/union/enum with anything else is not allowed
300: */
1.122 rillig 301: dcs->d_terr = true;
1.1 cgd 302: return;
303: }
304:
1.39 christos 305: if (t == COMPLEX) {
1.147 rillig 306: if (dcs->d_complex_mod == FLOAT)
1.39 christos 307: t = FCOMPLEX;
1.147 rillig 308: else if (dcs->d_complex_mod == DOUBLE)
1.39 christos 309: t = DCOMPLEX;
1.100 rillig 310: else {
1.104 rillig 311: /* invalid type for _Complex */
1.100 rillig 312: error(308);
313: t = DCOMPLEX; /* just as a fallback */
314: }
1.147 rillig 315: dcs->d_complex_mod = NOTSPEC;
1.39 christos 316: }
317:
1.147 rillig 318: if (t == LONG && dcs->d_rank_mod == LONG) {
1.1 cgd 319: /* "long long" or "long ... long" */
320: t = QUAD;
1.147 rillig 321: dcs->d_rank_mod = NOTSPEC;
1.9 jpo 322: if (!quadflg)
323: /* %s C does not support 'long long' */
1.106 rillig 324: c99ism(265, tflag ? "traditional" : "c89");
1.1 cgd 325: }
326:
1.3 jpo 327: if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
1.1 cgd 328: /* something like "typedef int a; a long ..." */
1.3 jpo 329: dcs->d_type = tdeferr(dcs->d_type, t);
1.1 cgd 330: return;
331: }
332:
333: /* now it can be only a combination of arithmetic types and void */
334: if (t == SIGNED || t == UNSIGN) {
1.147 rillig 335: /*
336: * remember specifiers "signed" & "unsigned" in
337: * dcs->d_sign_mod
338: */
339: if (dcs->d_sign_mod != NOTSPEC)
1.1 cgd 340: /*
1.25 wiz 341: * more than one "signed" and/or "unsigned"; print
1.1 cgd 342: * an error in deftyp()
343: */
1.122 rillig 344: dcs->d_terr = true;
1.147 rillig 345: dcs->d_sign_mod = t;
1.1 cgd 346: } else if (t == SHORT || t == LONG || t == QUAD) {
347: /*
348: * remember specifiers "short", "long" and "long long" in
1.147 rillig 349: * dcs->d_rank_mod
1.1 cgd 350: */
1.147 rillig 351: if (dcs->d_rank_mod != NOTSPEC)
1.1 cgd 352: /* more than one, print error in deftyp() */
1.122 rillig 353: dcs->d_terr = true;
1.147 rillig 354: dcs->d_rank_mod = t;
1.39 christos 355: } else if (t == FLOAT || t == DOUBLE) {
1.147 rillig 356: if (dcs->d_rank_mod == NOTSPEC || dcs->d_rank_mod == LONG) {
357: if (dcs->d_complex_mod != NOTSPEC
358: || (t == FLOAT && dcs->d_rank_mod == LONG))
1.122 rillig 359: dcs->d_terr = true;
1.147 rillig 360: dcs->d_complex_mod = t;
1.40 christos 361: } else {
1.147 rillig 362: if (dcs->d_abstract_type != NOTSPEC)
1.122 rillig 363: dcs->d_terr = true;
1.147 rillig 364: dcs->d_abstract_type = t;
1.40 christos 365: }
1.68 christos 366: } else if (t == PTR) {
367: dcs->d_type = tp;
1.1 cgd 368: } else {
369: /*
1.39 christos 370: * remember specifiers "void", "char", "int",
1.147 rillig 371: * or "_Complex" in dcs->d_abstract_type
1.1 cgd 372: */
1.147 rillig 373: if (dcs->d_abstract_type != NOTSPEC)
1.1 cgd 374: /* more than one, print error in deftyp() */
1.122 rillig 375: dcs->d_terr = true;
1.147 rillig 376: dcs->d_abstract_type = t;
1.1 cgd 377: }
378: }
379:
380: /*
381: * called if a list of declaration specifiers contains a typedef name
382: * and other specifiers (except struct, union, enum, typedef name)
383: */
384: static type_t *
1.22 lukem 385: tdeferr(type_t *td, tspec_t t)
1.1 cgd 386: {
387: tspec_t t2;
388:
389: t2 = td->t_tspec;
390:
391: switch (t) {
392: case SIGNED:
393: case UNSIGN:
394: if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG ||
395: t2 == QUAD) {
396: if (!tflag)
1.101 rillig 397: /* modifying typedef with '%s'; only ... */
1.1 cgd 398: warning(5, ttab[t].tt_name);
1.169 rillig 399: td = dup_type(gettyp(merge_type_specifiers(t2, t)));
1.122 rillig 400: td->t_typedef = true;
1.76 rillig 401: return td;
1.1 cgd 402: }
403: break;
404: case SHORT:
405: if (t2 == INT || t2 == UINT) {
1.101 rillig 406: /* modifying typedef with '%s'; only qualifiers ... */
1.1 cgd 407: warning(5, "short");
1.169 rillig 408: td = dup_type(gettyp(t2 == INT ? SHORT : USHORT));
1.122 rillig 409: td->t_typedef = true;
1.76 rillig 410: return td;
1.1 cgd 411: }
412: break;
413: case LONG:
414: if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG ||
1.42 matt 415: t2 == FLOAT || t2 == DOUBLE || t2 == DCOMPLEX) {
1.127 rillig 416: /* modifying typedef with '%s'; only qualifiers ... */
1.1 cgd 417: warning(5, "long");
418: if (t2 == INT) {
419: td = gettyp(LONG);
420: } else if (t2 == UINT) {
421: td = gettyp(ULONG);
422: } else if (t2 == LONG) {
423: td = gettyp(QUAD);
424: } else if (t2 == ULONG) {
425: td = gettyp(UQUAD);
426: } else if (t2 == FLOAT) {
427: td = gettyp(DOUBLE);
428: } else if (t2 == DOUBLE) {
429: td = gettyp(LDOUBLE);
1.42 matt 430: } else if (t2 == DCOMPLEX) {
431: td = gettyp(LCOMPLEX);
1.1 cgd 432: }
1.169 rillig 433: td = dup_type(td);
1.122 rillig 434: td->t_typedef = true;
1.76 rillig 435: return td;
1.1 cgd 436: }
437: break;
1.57 christos 438: /* LINTED206: (enumeration values not handled in switch) */
1.13 christos 439: case NOTSPEC:
440: case USHORT:
441: case UCHAR:
442: case SCHAR:
443: case CHAR:
1.34 yamt 444: case BOOL:
1.13 christos 445: case FUNC:
446: case ARRAY:
447: case PTR:
448: case ENUM:
449: case UNION:
450: case STRUCT:
451: case VOID:
452: case LDOUBLE:
1.39 christos 453: case FLOAT:
1.13 christos 454: case DOUBLE:
455: case UQUAD:
456: case QUAD:
1.69 christos 457: #ifdef INT128_SIZE
458: case UINT128:
459: case INT128:
460: #endif
1.13 christos 461: case ULONG:
462: case UINT:
463: case INT:
1.39 christos 464: case FCOMPLEX:
465: case DCOMPLEX:
1.42 matt 466: case LCOMPLEX:
1.39 christos 467: case COMPLEX:
1.28 thorpej 468: break;
1.1 cgd 469: }
470:
471: /* Anything other is not accepted. */
472:
1.122 rillig 473: dcs->d_terr = true;
1.76 rillig 474: return td;
1.1 cgd 475: }
476:
477: /*
478: * Remember the symbol of a typedef name (2nd arg) in a struct, union
479: * or enum tag if the typedef name is the first defined for this tag.
480: *
481: * If the tag is unnamed, the typdef name is used for identification
1.86 rillig 482: * of this tag in lint2. Although it's possible that more than one typedef
1.1 cgd 483: * name is defined for one tag, the first name defined should be unique
484: * if the tag is unnamed.
485: */
486: static void
1.22 lukem 487: settdsym(type_t *tp, sym_t *sym)
1.1 cgd 488: {
489: tspec_t t;
490:
491: if ((t = tp->t_tspec) == STRUCT || t == UNION) {
1.134 rillig 492: if (tp->t_str->sou_first_typedef == NULL)
493: tp->t_str->sou_first_typedef = sym;
1.1 cgd 494: } else if (t == ENUM) {
1.135 rillig 495: if (tp->t_enum->en_first_typedef == NULL)
496: tp->t_enum->en_first_typedef = sym;
1.1 cgd 497: }
498: }
499:
1.65 christos 500: static size_t
501: bitfieldsize(sym_t **mem)
502: {
503: size_t len = (*mem)->s_type->t_flen;
1.122 rillig 504: while (*mem != NULL && (*mem)->s_type->t_bitfield) {
1.65 christos 505: len += (*mem)->s_type->t_flen;
1.80 rillig 506: *mem = (*mem)->s_next;
1.65 christos 507: }
508: return ((len + INT_SIZE - 1) / INT_SIZE) * INT_SIZE;
509: }
510:
1.48 christos 511: static void
512: setpackedsize(type_t *tp)
1.47 christos 513: {
1.134 rillig 514: struct_or_union *sp;
1.47 christos 515: sym_t *mem;
516:
517: switch (tp->t_tspec) {
518: case STRUCT:
519: case UNION:
520: sp = tp->t_str;
1.149 rillig 521: sp->sou_size_in_bits = 0;
1.134 rillig 522: for (mem = sp->sou_first_member;
523: mem != NULL; mem = mem->s_next) {
1.108 rillig 524: if (mem->s_type->t_bitfield) {
1.149 rillig 525: sp->sou_size_in_bits += bitfieldsize(&mem);
1.51 christos 526: if (mem == NULL)
527: break;
528: }
1.148 rillig 529: size_t x = (size_t)type_size_in_bits(mem->s_type);
1.51 christos 530: if (tp->t_tspec == STRUCT)
1.149 rillig 531: sp->sou_size_in_bits += x;
532: else if (x > sp->sou_size_in_bits)
533: sp->sou_size_in_bits = x;
1.47 christos 534: }
535: break;
536: default:
1.94 rillig 537: /* %s attribute ignored for %s */
1.98 rillig 538: warning(326, "packed", type_name(tp));
1.47 christos 539: break;
540: }
541: }
542:
1.48 christos 543: void
544: addpacked(void)
545: {
1.49 christos 546: if (dcs->d_type == NULL)
1.122 rillig 547: dcs->d_packed = true;
1.49 christos 548: else
1.48 christos 549: setpackedsize(dcs->d_type);
550: }
551:
1.67 christos 552: void
1.77 rillig 553: add_attr_used(void)
1.67 christos 554: {
1.122 rillig 555: dcs->d_used = true;
1.67 christos 556: }
557:
1.1 cgd 558: /*
559: * Remember a qualifier which is part of the declaration specifiers
560: * (and not the declarator) in the top element of the declaration stack.
561: * Also detect multiple qualifiers of the same kind.
562:
1.21 lukem 563: * The remembered qualifier is used by deftyp() to construct the type
1.1 cgd 564: * for all declarators.
565: */
566: void
1.77 rillig 567: add_qualifier(tqual_t q)
1.1 cgd 568: {
1.22 lukem 569:
1.1 cgd 570: if (q == CONST) {
1.3 jpo 571: if (dcs->d_const) {
1.88 rillig 572: /* duplicate '%s' */
1.1 cgd 573: warning(10, "const");
574: }
1.122 rillig 575: dcs->d_const = true;
1.128 rillig 576: } else if (q == VOLATILE) {
1.3 jpo 577: if (dcs->d_volatile) {
1.88 rillig 578: /* duplicate '%s' */
1.1 cgd 579: warning(10, "volatile");
580: }
1.122 rillig 581: dcs->d_volatile = true;
1.128 rillig 582: } else {
583: lint_assert(q == RESTRICT || q == THREAD);
584: /* Silently ignore these qualifiers. */
1.1 cgd 585: }
586: }
587:
588: /*
589: * Go to the next declaration level (structs, nested structs, blocks,
590: * argument declaration lists ...)
591: */
592: void
1.160 rillig 593: begin_declaration_level(scl_t sc)
1.1 cgd 594: {
595: dinfo_t *di;
596:
597: /* put a new element on the declaration stack */
1.170 rillig 598: di = xcalloc(1, sizeof(*di));
1.82 rillig 599: di->d_next = dcs;
1.1 cgd 600: dcs = di;
1.3 jpo 601: di->d_ctx = sc;
602: di->d_ldlsym = &di->d_dlsyms;
1.47 christos 603: if (dflag)
1.162 christos 604: (void)printf("%s(%p %d)\n", __func__, dcs, (int)sc);
1.47 christos 605:
1.1 cgd 606: }
607:
608: /*
609: * Go back to previous declaration level
610: */
611: void
1.160 rillig 612: end_declaration_level(void)
1.1 cgd 613: {
614: dinfo_t *di;
615:
616: if (dflag)
1.162 christos 617: (void)printf("%s(%p %d)\n", __func__, dcs, (int)dcs->d_ctx);
1.1 cgd 618:
1.92 rillig 619: lint_assert(dcs->d_next != NULL);
1.1 cgd 620: di = dcs;
1.82 rillig 621: dcs = di->d_next;
1.3 jpo 622: switch (di->d_ctx) {
1.1 cgd 623: case MOS:
624: case MOU:
1.123 rillig 625: case CTCONST:
1.1 cgd 626: /*
627: * Symbols declared in (nested) structs or enums are
628: * part of the next level (they are removed from the
1.123 rillig 629: * symbol table if the symbols of the outer level are
1.73 rillig 630: * removed).
1.1 cgd 631: */
1.3 jpo 632: if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
633: dcs->d_ldlsym = di->d_ldlsym;
1.1 cgd 634: break;
635: case ARG:
636: /*
1.3 jpo 637: * All symbols in dcs->d_dlsyms are introduced in old style
1.1 cgd 638: * argument declarations (it's not clean, but possible).
639: * They are appended to the list of symbols declared in
640: * an old style argument identifier list or a new style
641: * parameter type list.
642: */
1.3 jpo 643: if (di->d_dlsyms != NULL) {
1.156 rillig 644: *di->d_ldlsym = dcs->d_func_proto_syms;
645: dcs->d_func_proto_syms = di->d_dlsyms;
1.1 cgd 646: }
647: break;
648: case ABSTRACT:
649: /*
650: * casts and sizeof
651: * Append all symbols declared in the abstract declaration
1.73 rillig 652: * to the list of symbols declared in the surrounding
653: * declaration or block.
1.1 cgd 654: * XXX I'm not sure whether they should be removed from the
655: * symbol table now or later.
656: */
1.3 jpo 657: if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
658: dcs->d_ldlsym = di->d_ldlsym;
1.1 cgd 659: break;
660: case AUTO:
661: /* check usage of local vars */
1.75 rillig 662: check_usage(di);
1.1 cgd 663: /* FALLTHROUGH */
1.137 rillig 664: case PROTO_ARG:
1.1 cgd 665: /* usage of arguments will be checked by funcend() */
1.3 jpo 666: rmsyms(di->d_dlsyms);
1.1 cgd 667: break;
1.97 rillig 668: case EXTERN:
669: /* there is nothing after external declarations */
670: /* FALLTHROUGH */
1.1 cgd 671: default:
1.124 rillig 672: lint_assert(/*CONSTCOND*/false);
1.1 cgd 673: }
674: free(di);
675: }
676:
677: /*
1.10 jpo 678: * Set flag d_asm in all declaration stack elements up to the
679: * outermost one.
680: *
681: * This is used to mark compound statements which have, possibly in
682: * nested compound statements, asm statements. For these compound
1.111 rillig 683: * statements no warnings about unused or uninitialized variables are
1.10 jpo 684: * printed.
685: *
686: * There is no need to clear d_asm in dinfo structs with context AUTO,
687: * because these structs are freed at the end of the compound statement.
1.62 dholland 688: * But it must be cleared in the outermost dinfo struct, which has
1.152 rillig 689: * context EXTERN. This could be done in clrtyp() and would work for C90,
690: * but not for C99 or C++ (due to mixed statements and declarations). Thus
1.77 rillig 691: * we clear it in global_clean_up_decl(), which is used to do some cleanup
692: * after global declarations/definitions.
1.10 jpo 693: */
694: void
1.22 lukem 695: setasm(void)
1.10 jpo 696: {
697: dinfo_t *di;
698:
1.82 rillig 699: for (di = dcs; di != NULL; di = di->d_next)
1.122 rillig 700: di->d_asm = true;
1.10 jpo 701: }
702:
703: /*
1.1 cgd 704: * Clean all elements of the top element of declaration stack which
705: * will be used by the next declaration
706: */
707: void
1.22 lukem 708: clrtyp(void)
1.1 cgd 709: {
1.22 lukem 710:
1.147 rillig 711: dcs->d_abstract_type = NOTSPEC;
712: dcs->d_complex_mod = NOTSPEC;
713: dcs->d_sign_mod = NOTSPEC;
714: dcs->d_rank_mod = NOTSPEC;
1.3 jpo 715: dcs->d_scl = NOSCL;
716: dcs->d_type = NULL;
1.122 rillig 717: dcs->d_const = false;
718: dcs->d_volatile = false;
719: dcs->d_inline = false;
720: dcs->d_mscl = false;
721: dcs->d_terr = false;
1.155 rillig 722: dcs->d_nonempty_decl = false;
1.122 rillig 723: dcs->d_notyp = false;
1.1 cgd 724: }
725:
1.150 rillig 726: static void
727: dcs_adjust_storage_class(void)
728: {
729: if (dcs->d_ctx == EXTERN) {
730: if (dcs->d_scl == REG || dcs->d_scl == AUTO) {
731: /* illegal storage class */
732: error(8);
733: dcs->d_scl = NOSCL;
734: }
735: } else if (dcs->d_ctx == ARG || dcs->d_ctx == PROTO_ARG) {
736: if (dcs->d_scl != NOSCL && dcs->d_scl != REG) {
737: /* only register valid as formal parameter storage... */
738: error(9);
739: dcs->d_scl = NOSCL;
740: }
741: }
742: }
743:
1.1 cgd 744: /*
1.73 rillig 745: * Create a type structure from the information gathered in
1.1 cgd 746: * the declaration stack.
747: * Complain about storage classes which are not possible in current
748: * context.
749: */
750: void
1.22 lukem 751: deftyp(void)
1.1 cgd 752: {
1.39 christos 753: tspec_t t, s, l, c;
1.1 cgd 754: type_t *tp;
755:
1.147 rillig 756: t = dcs->d_abstract_type; /* VOID, BOOL, CHAR, INT or COMPLEX */
757: c = dcs->d_complex_mod; /* FLOAT or DOUBLE */
758: s = dcs->d_sign_mod; /* SIGNED or UNSIGN */
759: l = dcs->d_rank_mod; /* SHORT, LONG or QUAD */
1.3 jpo 760: tp = dcs->d_type;
1.1 cgd 761:
1.68 christos 762: #ifdef DEBUG
1.98 rillig 763: printf("%s: %s\n", __func__, type_name(tp));
1.68 christos 764: #endif
1.39 christos 765: if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC &&
766: tp == NULL)
1.122 rillig 767: dcs->d_notyp = true;
1.43 dholland 768: if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) &&
769: tp == NULL)
1.39 christos 770: t = c;
1.1 cgd 771:
1.92 rillig 772: if (tp != NULL) {
773: lint_assert(t == NOTSPEC);
774: lint_assert(s == NOTSPEC);
775: lint_assert(l == NOTSPEC);
1.1 cgd 776: }
777:
778: if (tp == NULL) {
779: switch (t) {
1.34 yamt 780: case BOOL:
781: break;
1.1 cgd 782: case NOTSPEC:
783: t = INT;
784: /* FALLTHROUGH */
785: case INT:
786: if (s == NOTSPEC)
787: s = SIGNED;
788: break;
789: case CHAR:
790: if (l != NOTSPEC) {
1.122 rillig 791: dcs->d_terr = true;
1.1 cgd 792: l = NOTSPEC;
793: }
794: break;
795: case FLOAT:
796: if (l == LONG) {
797: l = NOTSPEC;
798: t = DOUBLE;
799: if (!tflag)
1.88 rillig 800: /* use 'double' instead of 'long ... */
1.1 cgd 801: warning(6);
802: }
803: break;
804: case DOUBLE:
805: if (l == LONG) {
1.59 christos 806: case LDOUBLE:
1.1 cgd 807: l = NOTSPEC;
808: t = LDOUBLE;
809: if (tflag)
810: /* 'long double' is illegal in ... */
811: warning(266);
812: }
813: break;
1.42 matt 814: case DCOMPLEX:
815: if (l == LONG) {
816: l = NOTSPEC;
817: t = LCOMPLEX;
818: if (tflag)
819: /* 'long double' is illegal in ... */
820: warning(266);
821: }
822: break;
1.1 cgd 823: case VOID:
1.39 christos 824: case FCOMPLEX:
1.42 matt 825: case LCOMPLEX:
1.1 cgd 826: break;
827: default:
1.164 rillig 828: INTERNAL_ERROR("deftyp(%s)", tspec_name(t));
1.1 cgd 829: }
830: if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) {
1.122 rillig 831: dcs->d_terr = true;
1.1 cgd 832: l = s = NOTSPEC;
833: }
834: if (l != NOTSPEC)
835: t = l;
1.77 rillig 836: dcs->d_type = gettyp(merge_type_specifiers(t, s));
1.1 cgd 837: }
838:
1.3 jpo 839: if (dcs->d_mscl) {
1.1 cgd 840: /* only one storage class allowed */
841: error(7);
842: }
1.3 jpo 843: if (dcs->d_terr) {
1.1 cgd 844: /* illegal type combination */
845: error(4);
846: }
847:
1.150 rillig 848: dcs_adjust_storage_class();
1.1 cgd 849:
1.3 jpo 850: if (dcs->d_const && dcs->d_type->t_const) {
1.92 rillig 851: lint_assert(dcs->d_type->t_typedef);
1.88 rillig 852: /* typedef already qualified with '%s' */
1.1 cgd 853: warning(68, "const");
854: }
1.3 jpo 855: if (dcs->d_volatile && dcs->d_type->t_volatile) {
1.92 rillig 856: lint_assert(dcs->d_type->t_typedef);
1.88 rillig 857: /* typedef already qualified with '%s' */
1.1 cgd 858: warning(68, "volatile");
859: }
860:
1.3 jpo 861: if (dcs->d_const || dcs->d_volatile) {
1.169 rillig 862: dcs->d_type = dup_type(dcs->d_type);
1.3 jpo 863: dcs->d_type->t_const |= dcs->d_const;
864: dcs->d_type->t_volatile |= dcs->d_volatile;
1.1 cgd 865: }
866: }
867:
868: /*
869: * Merge type specifiers (char, ..., long long, signed, unsigned).
870: */
871: static tspec_t
1.77 rillig 872: merge_type_specifiers(tspec_t t, tspec_t s)
1.1 cgd 873: {
1.22 lukem 874:
1.151 rillig 875: if (s != SIGNED && s != UNSIGN)
876: return t;
1.1 cgd 877:
1.151 rillig 878: if (t == CHAR)
879: return s == SIGNED ? SCHAR : UCHAR;
880: if (t == SHORT)
881: return s == SIGNED ? SHORT : USHORT;
882: if (t == INT)
883: return s == SIGNED ? INT : UINT;
884: if (t == LONG)
885: return s == SIGNED ? LONG : ULONG;
886: if (t == QUAD)
887: return s == SIGNED ? QUAD : UQUAD;
1.76 rillig 888: return t;
1.1 cgd 889: }
890:
891: /*
1.73 rillig 892: * Return the length of a type in bits.
1.1 cgd 893: *
1.111 rillig 894: * Printing a message if the outermost dimension of an array is 0 must
1.1 cgd 895: * be done by the caller. All other problems are reported by length()
896: * if name is not NULL.
897: */
898: int
1.121 rillig 899: length(const type_t *tp, const char *name)
1.1 cgd 900: {
901: int elem, elsz;
902:
903: elem = 1;
1.122 rillig 904: while (tp != NULL && tp->t_tspec == ARRAY) {
1.1 cgd 905: elem *= tp->t_dim;
906: tp = tp->t_subt;
907: }
1.18 christos 908: if (tp == NULL)
909: return -1;
910:
1.1 cgd 911: switch (tp->t_tspec) {
912: case FUNC:
913: /* compiler takes size of function */
1.164 rillig 914: INTERNAL_ERROR("%s", msgs[12]);
1.1 cgd 915: /* NOTREACHED */
916: case STRUCT:
917: case UNION:
1.132 rillig 918: if (is_incomplete(tp) && name != NULL) {
1.1 cgd 919: /* incomplete structure or union %s: %s */
1.134 rillig 920: error(31, tp->t_str->sou_tag->s_name, name);
1.1 cgd 921: }
1.149 rillig 922: elsz = tp->t_str->sou_size_in_bits;
1.1 cgd 923: break;
924: case ENUM:
1.132 rillig 925: if (is_incomplete(tp) && name != NULL) {
1.1 cgd 926: /* incomplete enum type: %s */
927: warning(13, name);
928: }
929: /* FALLTHROUGH */
930: default:
1.143 rillig 931: elsz = size_in_bits(tp->t_tspec);
1.1 cgd 932: if (elsz <= 0)
1.164 rillig 933: INTERNAL_ERROR("length(%d)", elsz);
1.1 cgd 934: break;
935: }
1.76 rillig 936: return elem * elsz;
1.1 cgd 937: }
938:
939: int
1.139 rillig 940: alignment_in_bits(const type_t *tp)
1.1 cgd 941: {
1.46 christos 942: size_t a;
1.1 cgd 943: tspec_t t;
944:
1.122 rillig 945: while (tp != NULL && tp->t_tspec == ARRAY)
1.1 cgd 946: tp = tp->t_subt;
947:
1.18 christos 948: if (tp == NULL)
949: return -1;
950:
1.1 cgd 951: if ((t = tp->t_tspec) == STRUCT || t == UNION) {
1.149 rillig 952: a = tp->t_str->sou_align_in_bits;
1.1 cgd 953: } else if (t == FUNC) {
954: /* compiler takes alignment of function */
955: error(14);
1.113 rillig 956: a = WORST_ALIGN(1) * CHAR_SIZE;
1.1 cgd 957: } else {
1.143 rillig 958: if ((a = size_in_bits(t)) == 0) {
1.113 rillig 959: a = CHAR_SIZE;
960: } else if (a > WORST_ALIGN(1) * CHAR_SIZE) {
961: a = WORST_ALIGN(1) * CHAR_SIZE;
1.1 cgd 962: }
963: }
1.113 rillig 964: lint_assert(a >= CHAR_SIZE);
965: lint_assert(a <= WORST_ALIGN(1) * CHAR_SIZE);
1.76 rillig 966: return a;
1.1 cgd 967: }
968:
969: /*
1.80 rillig 970: * Concatenate two lists of symbols by s_next. Used by declarations of
1.1 cgd 971: * struct/union/enum elements and parameters.
972: */
973: sym_t *
1.22 lukem 974: lnklst(sym_t *l1, sym_t *l2)
1.1 cgd 975: {
976: sym_t *l;
977:
978: if ((l = l1) == NULL)
1.76 rillig 979: return l2;
1.80 rillig 980: while (l1->s_next != NULL)
981: l1 = l1->s_next;
982: l1->s_next = l2;
1.76 rillig 983: return l;
1.1 cgd 984: }
985:
986: /*
987: * Check if the type of the given symbol is valid and print an error
988: * message if it is not.
989: *
990: * Invalid types are:
1.111 rillig 991: * - arrays of incomplete types or functions
1.1 cgd 992: * - functions returning arrays or functions
993: * - void types other than type of function or pointer
994: */
995: void
1.75 rillig 996: check_type(sym_t *sym)
1.1 cgd 997: {
998: tspec_t to, t;
999: type_t **tpp, *tp;
1000:
1001: tpp = &sym->s_type;
1002: to = NOTSPEC;
1003: while ((tp = *tpp) != NULL) {
1004: t = tp->t_tspec;
1005: /*
1006: * If this is the type of an old style function definition,
1007: * a better warning is printed in funcdef().
1008: */
1009: if (t == FUNC && !tp->t_proto &&
1010: !(to == NOTSPEC && sym->s_osdef)) {
1011: if (sflag && hflag)
1012: /* function declaration is not a prototype */
1013: warning(287);
1014: }
1015: if (to == FUNC) {
1016: if (t == FUNC || t == ARRAY) {
1017: /* function returns illegal type */
1018: error(15);
1019: if (t == FUNC) {
1.168 rillig 1020: *tpp = derive_type(*tpp, PTR);
1.1 cgd 1021: } else {
1.168 rillig 1022: *tpp = derive_type((*tpp)->t_subt, PTR);
1.1 cgd 1023: }
1024: return;
1025: } else if (tp->t_const || tp->t_volatile) {
1.137 rillig 1026: if (sflag) { /* XXX or better !tflag ? */
1.1 cgd 1027: /* function cannot return const... */
1028: warning(228);
1029: }
1030: }
1031: } if (to == ARRAY) {
1032: if (t == FUNC) {
1033: /* array of function is illegal */
1034: error(16);
1035: *tpp = gettyp(INT);
1036: return;
1037: } else if (t == ARRAY && tp->t_dim == 0) {
1038: /* null dimension */
1039: error(17);
1040: return;
1041: } else if (t == VOID) {
1.88 rillig 1042: /* illegal use of 'void' */
1.1 cgd 1043: error(18);
1044: *tpp = gettyp(INT);
1045: #if 0 /* errors are produced by length() */
1.132 rillig 1046: } else if (is_incomplete(tp)) {
1.1 cgd 1047: /* array of incomplete type */
1048: if (sflag) {
1.94 rillig 1049: /* array of incomplete type */
1.1 cgd 1050: error(301);
1051: } else {
1.94 rillig 1052: /* array of incomplete type */
1.1 cgd 1053: warning(301);
1054: }
1055: #endif
1056: }
1057: } else if (to == NOTSPEC && t == VOID) {
1.137 rillig 1058: if (dcs->d_ctx == PROTO_ARG) {
1.1 cgd 1059: if (sym->s_scl != ABSTRACT) {
1.92 rillig 1060: lint_assert(sym->s_name != unnamed);
1.88 rillig 1061: /* void param. cannot have name: %s */
1.1 cgd 1062: error(61, sym->s_name);
1063: *tpp = gettyp(INT);
1064: }
1.3 jpo 1065: } else if (dcs->d_ctx == ABSTRACT) {
1.1 cgd 1066: /* ok */
1067: } else if (sym->s_scl != TYPEDEF) {
1.133 rillig 1068: /* void type for '%s' */
1.1 cgd 1069: error(19, sym->s_name);
1070: *tpp = gettyp(INT);
1071: }
1072: }
1073: if (t == VOID && to != PTR) {
1074: if (tp->t_const || tp->t_volatile) {
1.88 rillig 1075: /* inappropriate qualifiers with 'void' */
1.1 cgd 1076: warning(69);
1.122 rillig 1077: tp->t_const = tp->t_volatile = false;
1.1 cgd 1078: }
1079: }
1080: tpp = &tp->t_subt;
1081: to = t;
1082: }
1083: }
1084:
1.142 rillig 1085: /*
1086: * In traditional C, the only portable type for bit-fields is unsigned int.
1087: *
1088: * In C90, the only allowed types for bit-fields are int, signed int and
1089: * unsigned int (3.5.2.1). There is no mention of implementation-defined
1090: * types.
1091: *
1092: * In C99, the only portable types for bit-fields are _Bool, signed int and
1093: * unsigned int (6.7.2.1p4). In addition, C99 allows "or some other
1094: * implementation-defined type".
1095: */
1.141 rillig 1096: static void
1.142 rillig 1097: declare_bit_field(sym_t *dsym, tspec_t *inout_t, type_t **const inout_tp)
1.141 rillig 1098: {
1099: tspec_t t = *inout_t;
1100: type_t *tp = *inout_tp;
1101:
1102: if (t == CHAR || t == UCHAR || t == SCHAR ||
1103: t == SHORT || t == USHORT || t == ENUM) {
1104: if (!bitfieldtype_ok) {
1105: if (sflag) {
1106: /* bit-field type '%s' invalid in ANSI C */
1107: warning(273, type_name(tp));
1108: } else if (pflag) {
1109: /* nonportable bit-field type */
1110: warning(34);
1111: }
1112: }
1.147 rillig 1113: } else if (t == INT && dcs->d_sign_mod == NOTSPEC) {
1.141 rillig 1114: if (pflag && !bitfieldtype_ok) {
1115: /* nonportable bit-field type */
1116: warning(34);
1117: }
1118: } else if (t != INT && t != UINT && t != BOOL) {
1119: /*
1120: * Non-integer types are always illegal for bitfields,
1121: * regardless of BITFIELDTYPE. Integer types not dealt with
1122: * above are okay only if BITFIELDTYPE is in effect.
1123: */
1124: if (!bitfieldtype_ok || !is_integer(t)) {
1125: /* illegal bit-field type '%s' */
1126: warning(35, type_name(tp));
1127: int sz = tp->t_flen;
1.169 rillig 1128: dsym->s_type = tp = dup_type(gettyp(t = INT));
1.143 rillig 1129: if ((tp->t_flen = sz) > size_in_bits(t))
1130: tp->t_flen = size_in_bits(t);
1.141 rillig 1131: }
1132: }
1133:
1.143 rillig 1134: if (tp->t_flen < 0 || tp->t_flen > (ssize_t)size_in_bits(t)) {
1.141 rillig 1135: /* illegal bit-field size: %d */
1136: error(36, tp->t_flen);
1.143 rillig 1137: tp->t_flen = size_in_bits(t);
1.141 rillig 1138: } else if (tp->t_flen == 0 && dsym->s_name != unnamed) {
1139: /* zero size bit-field */
1140: error(37);
1.143 rillig 1141: tp->t_flen = size_in_bits(t);
1.141 rillig 1142: }
1143: if (dsym->s_scl == MOU) {
1144: /* illegal use of bit-field */
1145: error(41);
1146: dsym->s_type->t_bitfield = false;
1147: dsym->s_bitfield = false;
1148: }
1149:
1150: *inout_t = t;
1151: *inout_tp = tp;
1152: }
1153:
1.1 cgd 1154: /*
1155: * Process the declarator of a struct/union element.
1156: */
1157: sym_t *
1.75 rillig 1158: declarator_1_struct_union(sym_t *dsym)
1.1 cgd 1159: {
1160: type_t *tp;
1161: tspec_t t;
1.141 rillig 1162: int sz;
1163: int o = 0; /* Appease GCC */
1.1 cgd 1164:
1.153 rillig 1165: lint_assert(dsym->s_scl == MOS || dsym->s_scl == MOU);
1.1 cgd 1166:
1.154 rillig 1167: if (dcs->d_redeclared_symbol != NULL) {
1.92 rillig 1168: /* should be ensured by storesym() */
1.154 rillig 1169: lint_assert(dcs->d_redeclared_symbol->s_scl == MOS ||
1170: dcs->d_redeclared_symbol->s_scl == MOU);
1.153 rillig 1171:
1.154 rillig 1172: if (dsym->s_styp == dcs->d_redeclared_symbol->s_styp) {
1.1 cgd 1173: /* duplicate member name: %s */
1174: error(33, dsym->s_name);
1.154 rillig 1175: rmsym(dcs->d_redeclared_symbol);
1.1 cgd 1176: }
1177: }
1178:
1.75 rillig 1179: check_type(dsym);
1.1 cgd 1180:
1181: t = (tp = dsym->s_type)->t_tspec;
1182:
1.83 rillig 1183: if (dsym->s_bitfield) {
1.141 rillig 1184: declare_bit_field(dsym, &t, &tp);
1.1 cgd 1185: } else if (t == FUNC) {
1186: /* function illegal in structure or union */
1187: error(38);
1.168 rillig 1188: dsym->s_type = tp = derive_type(tp, t = PTR);
1.1 cgd 1189: }
1190:
1191: /*
1192: * bit-fields of length 0 are not warned about because length()
1193: * does not return the length of the bit-field but the length
1.86 rillig 1194: * of the type the bit-field is packed in (it's ok)
1.1 cgd 1195: */
1196: if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1.36 christos 1197: if (t == ARRAY && dsym->s_type->t_dim == 0) {
1.89 rillig 1198: /* zero sized array in struct is a C99 extension: %s */
1.36 christos 1199: c99ism(39, dsym->s_name);
1.1 cgd 1200: }
1201: }
1202:
1.3 jpo 1203: if (dcs->d_ctx == MOU) {
1204: o = dcs->d_offset;
1205: dcs->d_offset = 0;
1.1 cgd 1206: }
1.83 rillig 1207: if (dsym->s_bitfield) {
1.139 rillig 1208: align(alignment_in_bits(tp), tp->t_flen);
1.143 rillig 1209: dsym->s_value.v_quad =
1210: (dcs->d_offset / size_in_bits(t)) * size_in_bits(t);
1.3 jpo 1211: tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1212: dcs->d_offset += tp->t_flen;
1.1 cgd 1213: } else {
1.139 rillig 1214: align(alignment_in_bits(tp), 0);
1.3 jpo 1215: dsym->s_value.v_quad = dcs->d_offset;
1216: dcs->d_offset += sz;
1.1 cgd 1217: }
1.3 jpo 1218: if (dcs->d_ctx == MOU) {
1219: if (o > dcs->d_offset)
1220: dcs->d_offset = o;
1.1 cgd 1221: }
1222:
1.124 rillig 1223: check_function_definition(dsym, false);
1.26 thorpej 1224:
1225: /*
1226: * Clear the BITFIELDTYPE indicator after processing each
1227: * structure element.
1228: */
1.103 rillig 1229: bitfieldtype_ok = false;
1.1 cgd 1230:
1.76 rillig 1231: return dsym;
1.1 cgd 1232: }
1233:
1234: /*
1235: * Aligns next structure element as required.
1236: *
1237: * al contains the required alignment, len the length of a bit-field.
1238: */
1239: static void
1.22 lukem 1240: align(int al, int len)
1.1 cgd 1241: {
1242: int no;
1243:
1244: /*
1245: * The alignment of the current element becomes the alignment of
1246: * the struct/union if it is larger than the current alignment
1247: * of the struct/union.
1248: */
1.3 jpo 1249: if (al > dcs->d_stralign)
1250: dcs->d_stralign = al;
1.22 lukem 1251:
1.3 jpo 1252: no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1253: if (len == 0 || dcs->d_offset + len > no)
1254: dcs->d_offset = no;
1.1 cgd 1255: }
1256:
1257: /*
1258: * Remember the width of the field in its type structure.
1259: */
1260: sym_t *
1.22 lukem 1261: bitfield(sym_t *dsym, int len)
1.1 cgd 1262: {
1.22 lukem 1263:
1.1 cgd 1264: if (dsym == NULL) {
1.170 rillig 1265: dsym = getblk(sizeof(*dsym));
1.1 cgd 1266: dsym->s_name = unnamed;
1.81 rillig 1267: dsym->s_kind = FMEMBER;
1.1 cgd 1268: dsym->s_scl = MOS;
1.20 sommerfe 1269: dsym->s_type = gettyp(UINT);
1.144 rillig 1270: dsym->s_block_level = -1;
1.1 cgd 1271: }
1.169 rillig 1272: dsym->s_type = dup_type(dsym->s_type);
1.108 rillig 1273: dsym->s_type->t_bitfield = true;
1.1 cgd 1274: dsym->s_type->t_flen = len;
1.122 rillig 1275: dsym->s_bitfield = true;
1.76 rillig 1276: return dsym;
1.1 cgd 1277: }
1278:
1279: /*
1.79 rillig 1280: * Collect information about a sequence of asterisks and qualifiers in a
1281: * list of type pqinf_t.
1282: * Qualifiers always refer to the left asterisk.
1283: * The rightmost asterisk will be at the top of the list.
1.1 cgd 1284: */
1285: pqinf_t *
1.75 rillig 1286: merge_pointers_and_qualifiers(pqinf_t *p1, pqinf_t *p2)
1.1 cgd 1287: {
1288: pqinf_t *p;
1289:
1290: if (p2->p_pcnt != 0) {
1291: /* left '*' at the end of the list */
1.82 rillig 1292: for (p = p2; p->p_next != NULL; p = p->p_next)
1.22 lukem 1293: continue;
1.82 rillig 1294: p->p_next = p1;
1.76 rillig 1295: return p2;
1.1 cgd 1296: } else {
1297: if (p2->p_const) {
1298: if (p1->p_const) {
1.88 rillig 1299: /* duplicate '%s' */
1.1 cgd 1300: warning(10, "const");
1301: }
1.122 rillig 1302: p1->p_const = true;
1.1 cgd 1303: }
1304: if (p2->p_volatile) {
1305: if (p1->p_volatile) {
1.88 rillig 1306: /* duplicate '%s' */
1.1 cgd 1307: warning(10, "volatile");
1308: }
1.122 rillig 1309: p1->p_volatile = true;
1.1 cgd 1310: }
1311: free(p2);
1.76 rillig 1312: return p1;
1.1 cgd 1313: }
1314: }
1315:
1316: /*
1.73 rillig 1317: * The following 3 functions extend the type of a declarator with
1.1 cgd 1318: * pointer, function and array types.
1319: *
1.130 rillig 1320: * The current type is the type built by deftyp() (dcs->d_type) and
1.1 cgd 1321: * pointer, function and array types already added for this
1322: * declarator. The new type extension is inserted between both.
1323: */
1324: sym_t *
1.75 rillig 1325: add_pointer(sym_t *decl, pqinf_t *pi)
1.1 cgd 1326: {
1327: type_t **tpp, *tp;
1328: pqinf_t *npi;
1329:
1330: tpp = &decl->s_type;
1.122 rillig 1331: while (*tpp != NULL && *tpp != dcs->d_type)
1.1 cgd 1332: tpp = &(*tpp)->t_subt;
1.18 christos 1333: if (*tpp == NULL)
1334: return decl;
1.1 cgd 1335:
1336: while (pi != NULL) {
1.170 rillig 1337: *tpp = tp = getblk(sizeof(*tp));
1.1 cgd 1338: tp->t_tspec = PTR;
1339: tp->t_const = pi->p_const;
1340: tp->t_volatile = pi->p_volatile;
1.3 jpo 1341: *(tpp = &tp->t_subt) = dcs->d_type;
1.82 rillig 1342: npi = pi->p_next;
1.1 cgd 1343: free(pi);
1344: pi = npi;
1345: }
1.76 rillig 1346: return decl;
1.1 cgd 1347: }
1348:
1349: /*
1.157 rillig 1350: * If a dimension was specified, dim is true, otherwise false
1.1 cgd 1351: * n is the specified dimension
1352: */
1353: sym_t *
1.122 rillig 1354: add_array(sym_t *decl, bool dim, int n)
1.1 cgd 1355: {
1356: type_t **tpp, *tp;
1357:
1358: tpp = &decl->s_type;
1.122 rillig 1359: while (*tpp != NULL && *tpp != dcs->d_type)
1.1 cgd 1360: tpp = &(*tpp)->t_subt;
1.18 christos 1361: if (*tpp == NULL)
1362: return decl;
1.1 cgd 1363:
1.170 rillig 1364: *tpp = tp = getblk(sizeof(*tp));
1.1 cgd 1365: tp->t_tspec = ARRAY;
1.3 jpo 1366: tp->t_subt = dcs->d_type;
1.1 cgd 1367: tp->t_dim = n;
1368:
1369: if (n < 0) {
1.88 rillig 1370: /* negative array dimension (%d) */
1.36 christos 1371: error(20, n);
1.1 cgd 1372: n = 0;
1373: } else if (n == 0 && dim) {
1.89 rillig 1374: /* zero sized array is a C99 extension */
1.90 rillig 1375: c99ism(322);
1.1 cgd 1376: } else if (n == 0 && !dim) {
1.124 rillig 1377: setcomplete(tp, false);
1.1 cgd 1378: }
1379:
1.76 rillig 1380: return decl;
1.1 cgd 1381: }
1382:
1383: sym_t *
1.75 rillig 1384: add_function(sym_t *decl, sym_t *args)
1.1 cgd 1385: {
1386: type_t **tpp, *tp;
1387:
1.3 jpo 1388: if (dcs->d_proto) {
1.1 cgd 1389: if (tflag)
1390: /* function prototypes are illegal in traditional C */
1391: warning(270);
1.75 rillig 1392: args = new_style_function(decl, args);
1.1 cgd 1393: } else {
1.75 rillig 1394: old_style_function(decl, args);
1.1 cgd 1395: }
1396:
1397: /*
1.160 rillig 1398: * The symbols are removed from the symbol table by
1399: * end_declaration_level after add_function. To be able to restore
1400: * them if this is a function definition, a pointer to the list of all
1401: * symbols is stored in dcs->d_next->d_func_proto_syms. Also a list of
1402: * the arguments (concatenated by s_next) is stored in
1403: * dcs->d_next->d_func_args. (dcs->d_next must be used because *dcs is
1404: * the declaration stack element created for the list of params and is
1405: * removed after add_function.)
1.1 cgd 1406: */
1.82 rillig 1407: if (dcs->d_next->d_ctx == EXTERN &&
1408: decl->s_type == dcs->d_next->d_type) {
1.156 rillig 1409: dcs->d_next->d_func_proto_syms = dcs->d_dlsyms;
1410: dcs->d_next->d_func_args = args;
1.1 cgd 1411: }
1412:
1413: tpp = &decl->s_type;
1.122 rillig 1414: while (*tpp != NULL && *tpp != dcs->d_next->d_type)
1.1 cgd 1415: tpp = &(*tpp)->t_subt;
1.18 christos 1416: if (*tpp == NULL)
1417: return decl;
1.1 cgd 1418:
1.170 rillig 1419: *tpp = tp = getblk(sizeof(*tp));
1.1 cgd 1420: tp->t_tspec = FUNC;
1.82 rillig 1421: tp->t_subt = dcs->d_next->d_type;
1.126 rillig 1422: if ((tp->t_proto = dcs->d_proto) != false)
1.1 cgd 1423: tp->t_args = args;
1.3 jpo 1424: tp->t_vararg = dcs->d_vararg;
1.1 cgd 1425:
1.76 rillig 1426: return decl;
1.1 cgd 1427: }
1428:
1429: /*
1430: * Called for new style function declarations.
1431: */
1432: /* ARGSUSED */
1433: static sym_t *
1.75 rillig 1434: new_style_function(sym_t *decl, sym_t *args)
1.1 cgd 1435: {
1436: sym_t *arg, *sym;
1437: scl_t sc;
1438: int n;
1439:
1440: /*
1441: * Declarations of structs/unions/enums in param lists are legal,
1442: * but senseless.
1443: */
1.3 jpo 1444: for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1.1 cgd 1445: sc = sym->s_scl;
1.137 rillig 1446: if (sc == STRUCT_TAG || sc == UNION_TAG || sc == ENUM_TAG) {
1.1 cgd 1447: /* dubious tag declaration: %s %s */
1.77 rillig 1448: warning(85, storage_class_name(sc), sym->s_name);
1.1 cgd 1449: }
1450: }
1451:
1452: n = 1;
1.80 rillig 1453: for (arg = args; arg != NULL; arg = arg->s_next) {
1.1 cgd 1454: if (arg->s_type->t_tspec == VOID) {
1.80 rillig 1455: if (n > 1 || arg->s_next != NULL) {
1.88 rillig 1456: /* void must be sole parameter */
1.1 cgd 1457: error(60);
1458: arg->s_type = gettyp(INT);
1459: }
1460: }
1461: n++;
1462: }
1463:
1464: /* return NULL if first param is VOID */
1.76 rillig 1465: return args != NULL && args->s_type->t_tspec != VOID ? args : NULL;
1.1 cgd 1466: }
1467:
1468: /*
1469: * Called for old style function declarations.
1470: */
1471: static void
1.75 rillig 1472: old_style_function(sym_t *decl, sym_t *args)
1.1 cgd 1473: {
1.22 lukem 1474:
1.1 cgd 1475: /*
1476: * Remember list of params only if this is really seams to be
1477: * a function definition.
1478: */
1.82 rillig 1479: if (dcs->d_next->d_ctx == EXTERN &&
1480: decl->s_type == dcs->d_next->d_type) {
1.1 cgd 1481: /*
1482: * We assume that this becomes a function definition. If
1.86 rillig 1483: * we are wrong, it's corrected in check_function_definition().
1.1 cgd 1484: */
1485: if (args != NULL) {
1.122 rillig 1486: decl->s_osdef = true;
1.1 cgd 1487: decl->s_args = args;
1488: }
1489: } else {
1490: if (args != NULL)
1491: /* function prototype parameters must have types */
1492: warning(62);
1493: }
1494: }
1495:
1496: /*
1.130 rillig 1497: * Lists of identifiers in functions declarations are allowed only if
1498: * it's also a function definition. If this is not the case, print an
1.1 cgd 1499: * error message.
1500: */
1501: void
1.122 rillig 1502: check_function_definition(sym_t *sym, bool msg)
1.1 cgd 1503: {
1.22 lukem 1504:
1.1 cgd 1505: if (sym->s_osdef) {
1506: if (msg) {
1507: /* incomplete or misplaced function definition */
1508: error(22);
1509: }
1.122 rillig 1510: sym->s_osdef = false;
1.1 cgd 1511: sym->s_args = NULL;
1512: }
1513: }
1514:
1515: /*
1516: * Process the name in a declarator.
1.73 rillig 1517: * The symbol gets one of the storage classes EXTERN, STATIC, AUTO or
1.1 cgd 1518: * TYPEDEF.
1.75 rillig 1519: * s_def and s_reg are valid after declarator_name().
1.1 cgd 1520: */
1521: sym_t *
1.75 rillig 1522: declarator_name(sym_t *sym)
1.1 cgd 1523: {
1.14 mycroft 1524: scl_t sc = NOSCL;
1.1 cgd 1525:
1526: if (sym->s_scl == NOSCL) {
1.154 rillig 1527: dcs->d_redeclared_symbol = NULL;
1.1 cgd 1528: } else if (sym->s_defarg) {
1.122 rillig 1529: sym->s_defarg = false;
1.154 rillig 1530: dcs->d_redeclared_symbol = NULL;
1.1 cgd 1531: } else {
1.154 rillig 1532: dcs->d_redeclared_symbol = sym;
1.1 cgd 1533: sym = pushdown(sym);
1534: }
1535:
1.3 jpo 1536: switch (dcs->d_ctx) {
1.1 cgd 1537: case MOS:
1538: case MOU:
1.73 rillig 1539: /* Set parent */
1.3 jpo 1540: sym->s_styp = dcs->d_tagtyp->t_str;
1.1 cgd 1541: sym->s_def = DEF;
1542: sym->s_value.v_tspec = INT;
1.3 jpo 1543: sc = dcs->d_ctx;
1.1 cgd 1544: break;
1545: case EXTERN:
1546: /*
1547: * static and external symbols without "extern" are
1548: * considered to be tentative defined, external
1549: * symbols with "extern" are declared, and typedef names
1550: * are defined. Tentative defined and declared symbols
1551: * may become defined if an initializer is present or
1552: * this is a function definition.
1553: */
1.3 jpo 1554: if ((sc = dcs->d_scl) == NOSCL) {
1.1 cgd 1555: sc = EXTERN;
1556: sym->s_def = TDEF;
1557: } else if (sc == STATIC) {
1558: sym->s_def = TDEF;
1559: } else if (sc == TYPEDEF) {
1560: sym->s_def = DEF;
1.93 rillig 1561: } else {
1562: lint_assert(sc == EXTERN);
1.1 cgd 1563: sym->s_def = DECL;
1564: }
1565: break;
1.137 rillig 1566: case PROTO_ARG:
1.122 rillig 1567: sym->s_arg = true;
1.1 cgd 1568: /* FALLTHROUGH */
1569: case ARG:
1.3 jpo 1570: if ((sc = dcs->d_scl) == NOSCL) {
1.1 cgd 1571: sc = AUTO;
1.93 rillig 1572: } else {
1573: lint_assert(sc == REG);
1.122 rillig 1574: sym->s_reg = true;
1.1 cgd 1575: sc = AUTO;
1576: }
1577: sym->s_def = DEF;
1578: break;
1579: case AUTO:
1.3 jpo 1580: if ((sc = dcs->d_scl) == NOSCL) {
1.1 cgd 1581: /*
1582: * XXX somewhat ugly because we dont know whether
1583: * this is AUTO or EXTERN (functions). If we are
1.116 rillig 1584: * wrong it must be corrected in declare_local(),
1585: * where we have the necessary type information.
1.1 cgd 1586: */
1587: sc = AUTO;
1588: sym->s_def = DEF;
1589: } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1590: sym->s_def = DEF;
1591: } else if (sc == REG) {
1.122 rillig 1592: sym->s_reg = true;
1.1 cgd 1593: sc = AUTO;
1594: sym->s_def = DEF;
1.93 rillig 1595: } else {
1596: lint_assert(sc == EXTERN);
1.1 cgd 1597: sym->s_def = DECL;
1598: }
1599: break;
1600: default:
1.124 rillig 1601: lint_assert(/*CONSTCOND*/false);
1.1 cgd 1602: }
1603: sym->s_scl = sc;
1604:
1.3 jpo 1605: sym->s_type = dcs->d_type;
1.1 cgd 1606:
1.156 rillig 1607: dcs->d_func_proto_syms = NULL;
1.1 cgd 1608:
1.76 rillig 1609: return sym;
1.1 cgd 1610: }
1611:
1612: /*
1.130 rillig 1613: * Process a name in the list of formal parameters in an old style function
1.1 cgd 1614: * definition.
1615: */
1616: sym_t *
1.75 rillig 1617: old_style_function_name(sym_t *sym)
1.1 cgd 1618: {
1.22 lukem 1619:
1.1 cgd 1620: if (sym->s_scl != NOSCL) {
1.144 rillig 1621: if (block_level == sym->s_block_level) {
1.1 cgd 1622: /* redeclaration of formal parameter %s */
1623: error(21, sym->s_name);
1.92 rillig 1624: lint_assert(sym->s_defarg);
1.1 cgd 1625: }
1626: sym = pushdown(sym);
1627: }
1628: sym->s_type = gettyp(INT);
1629: sym->s_scl = AUTO;
1630: sym->s_def = DEF;
1.122 rillig 1631: sym->s_defarg = sym->s_arg = true;
1.76 rillig 1632: return sym;
1.1 cgd 1633: }
1634:
1635: /*
1636: * Create the type of a tag.
1637: *
1638: * tag points to the symbol table entry of the tag
1639: * kind is the kind of the tag (STRUCT/UNION/ENUM)
1.122 rillig 1640: * decl is true if the type of the tag will be completed in this declaration
1.1 cgd 1641: * (the following token is T_LBRACE)
1.122 rillig 1642: * semi is true if the following token is T_SEMI
1.1 cgd 1643: */
1644: type_t *
1.122 rillig 1645: mktag(sym_t *tag, tspec_t kind, bool decl, bool semi)
1.1 cgd 1646: {
1.130 rillig 1647: scl_t scl;
1.1 cgd 1648: type_t *tp;
1649:
1650: if (kind == STRUCT) {
1.137 rillig 1651: scl = STRUCT_TAG;
1.1 cgd 1652: } else if (kind == UNION) {
1.137 rillig 1653: scl = UNION_TAG;
1.92 rillig 1654: } else {
1655: lint_assert(kind == ENUM);
1.137 rillig 1656: scl = ENUM_TAG;
1.1 cgd 1657: }
1658:
1659: if (tag != NULL) {
1660: if (tag->s_scl != NOSCL) {
1661: tag = newtag(tag, scl, decl, semi);
1662: } else {
1663: /* a new tag, no empty declaration */
1.155 rillig 1664: dcs->d_next->d_nonempty_decl = true;
1.137 rillig 1665: if (scl == ENUM_TAG && !decl) {
1.1 cgd 1666: if (!tflag && (sflag || pflag))
1667: /* forward reference to enum type */
1668: warning(42);
1669: }
1670: }
1671: if (tag->s_scl == NOSCL) {
1672: tag->s_scl = scl;
1.170 rillig 1673: tag->s_type = tp = getblk(sizeof(*tp));
1.109 rillig 1674: tp->t_packed = dcs->d_packed;
1.1 cgd 1675: } else {
1676: tp = tag->s_type;
1677: }
1678: } else {
1.170 rillig 1679: tag = getblk(sizeof(*tag));
1.1 cgd 1680: tag->s_name = unnamed;
1.84 rillig 1681: UNIQUE_CURR_POS(tag->s_def_pos);
1.1 cgd 1682: tag->s_kind = FTAG;
1683: tag->s_scl = scl;
1.144 rillig 1684: tag->s_block_level = -1;
1.170 rillig 1685: tag->s_type = tp = getblk(sizeof(*tp));
1.109 rillig 1686: tp->t_packed = dcs->d_packed;
1.155 rillig 1687: dcs->d_next->d_nonempty_decl = true;
1.1 cgd 1688: }
1689:
1690: if (tp->t_tspec == NOTSPEC) {
1691: tp->t_tspec = kind;
1692: if (kind != ENUM) {
1.170 rillig 1693: tp->t_str = getblk(sizeof(*tp->t_str));
1.149 rillig 1694: tp->t_str->sou_align_in_bits = CHAR_SIZE;
1.134 rillig 1695: tp->t_str->sou_tag = tag;
1.1 cgd 1696: } else {
1.136 rillig 1697: tp->t_is_enum = true;
1.170 rillig 1698: tp->t_enum = getblk(sizeof(*tp->t_enum));
1.135 rillig 1699: tp->t_enum->en_tag = tag;
1.1 cgd 1700: }
1.124 rillig 1701: setcomplete(tp, false);
1.1 cgd 1702: }
1.76 rillig 1703: return tp;
1.1 cgd 1704: }
1705:
1706: /*
1707: * Checks all possible cases of tag redeclarations.
1.122 rillig 1708: * decl is true if T_LBRACE follows
1709: * semi is true if T_SEMI follows
1.1 cgd 1710: */
1711: static sym_t *
1.122 rillig 1712: newtag(sym_t *tag, scl_t scl, bool decl, bool semi)
1.1 cgd 1713: {
1.22 lukem 1714:
1.144 rillig 1715: if (tag->s_block_level < block_level) {
1.1 cgd 1716: if (semi) {
1717: /* "struct a;" */
1718: if (!tflag) {
1719: if (!sflag)
1720: /* decl. introduces new type ... */
1.77 rillig 1721: warning(44, storage_class_name(scl),
1722: tag->s_name);
1.1 cgd 1723: tag = pushdown(tag);
1724: } else if (tag->s_scl != scl) {
1.88 rillig 1725: /* base type is really '%s %s' */
1.77 rillig 1726: warning(45, storage_class_name(tag->s_scl),
1727: tag->s_name);
1.1 cgd 1728: }
1.155 rillig 1729: dcs->d_next->d_nonempty_decl = true;
1.1 cgd 1730: } else if (decl) {
1.22 lukem 1731: /* "struct a { ... } " */
1.1 cgd 1732: if (hflag)
1733: /* redefinition hides earlier one: %s */
1734: warning(43, tag->s_name);
1735: tag = pushdown(tag);
1.155 rillig 1736: dcs->d_next->d_nonempty_decl = true;
1.1 cgd 1737: } else if (tag->s_scl != scl) {
1.88 rillig 1738: /* base type is really '%s %s' */
1.77 rillig 1739: warning(45, storage_class_name(tag->s_scl),
1740: tag->s_name);
1.1 cgd 1741: /* declaration introduces new type in ANSI C: %s %s */
1.77 rillig 1742: if (!sflag) {
1.94 rillig 1743: /* decl. introduces new type in ANSI C: %s %s */
1.77 rillig 1744: warning(44, storage_class_name(scl),
1745: tag->s_name);
1746: }
1.1 cgd 1747: tag = pushdown(tag);
1.155 rillig 1748: dcs->d_next->d_nonempty_decl = true;
1.1 cgd 1749: }
1750: } else {
1751: if (tag->s_scl != scl) {
1752: /* (%s) tag redeclared */
1.77 rillig 1753: error(46, storage_class_name(tag->s_scl));
1.75 rillig 1754: print_previous_declaration(-1, tag);
1.1 cgd 1755: tag = pushdown(tag);
1.155 rillig 1756: dcs->d_next->d_nonempty_decl = true;
1.132 rillig 1757: } else if (decl && !is_incomplete(tag->s_type)) {
1.1 cgd 1758: /* (%s) tag redeclared */
1.77 rillig 1759: error(46, storage_class_name(tag->s_scl));
1.75 rillig 1760: print_previous_declaration(-1, tag);
1.1 cgd 1761: tag = pushdown(tag);
1.155 rillig 1762: dcs->d_next->d_nonempty_decl = true;
1.1 cgd 1763: } else if (semi || decl) {
1.155 rillig 1764: dcs->d_next->d_nonempty_decl = true;
1.1 cgd 1765: }
1766: }
1.76 rillig 1767: return tag;
1.1 cgd 1768: }
1769:
1770: const char *
1.77 rillig 1771: storage_class_name(scl_t sc)
1.1 cgd 1772: {
1773: const char *s;
1774:
1775: switch (sc) {
1776: case EXTERN: s = "extern"; break;
1777: case STATIC: s = "static"; break;
1778: case AUTO: s = "auto"; break;
1779: case REG: s = "register"; break;
1780: case TYPEDEF: s = "typedef"; break;
1.137 rillig 1781: case STRUCT_TAG:s = "struct"; break;
1782: case UNION_TAG: s = "union"; break;
1783: case ENUM_TAG: s = "enum"; break;
1.124 rillig 1784: default: lint_assert(/*CONSTCOND*/false);
1.1 cgd 1785: }
1.76 rillig 1786: return s;
1.1 cgd 1787: }
1788:
1789: /*
1.78 rillig 1790: * tp points to the type of the tag, fmem to the list of members.
1.1 cgd 1791: */
1792: type_t *
1.78 rillig 1793: complete_tag_struct_or_union(type_t *tp, sym_t *fmem)
1.1 cgd 1794: {
1795: tspec_t t;
1.134 rillig 1796: struct_or_union *sp;
1.1 cgd 1797: int n;
1798: sym_t *mem;
1799:
1.124 rillig 1800: setcomplete(tp, true);
1.1 cgd 1801:
1.78 rillig 1802: t = tp->t_tspec;
1803: align(dcs->d_stralign, 0);
1804: sp = tp->t_str;
1.149 rillig 1805: sp->sou_align_in_bits = dcs->d_stralign;
1.134 rillig 1806: sp->sou_first_member = fmem;
1.109 rillig 1807: if (tp->t_packed)
1.78 rillig 1808: setpackedsize(tp);
1809: else
1.149 rillig 1810: sp->sou_size_in_bits = dcs->d_offset;
1.78 rillig 1811:
1.149 rillig 1812: if (sp->sou_size_in_bits == 0) {
1.106 rillig 1813: /* zero sized %s is a C9X feature */
1814: c99ism(47, ttab[t].tt_name);
1.78 rillig 1815: }
1.63 christos 1816:
1.78 rillig 1817: n = 0;
1.80 rillig 1818: for (mem = fmem; mem != NULL; mem = mem->s_next) {
1.78 rillig 1819: /* bind anonymous members to the structure */
1820: if (mem->s_styp == NULL) {
1821: mem->s_styp = sp;
1.108 rillig 1822: if (mem->s_type->t_bitfield) {
1.149 rillig 1823: sp->sou_size_in_bits += bitfieldsize(&mem);
1.78 rillig 1824: if (mem == NULL)
1825: break;
1.63 christos 1826: }
1.149 rillig 1827: sp->sou_size_in_bits += type_size_in_bits(mem->s_type);
1.63 christos 1828: }
1.78 rillig 1829: if (mem->s_name != unnamed)
1830: n++;
1831: }
1.63 christos 1832:
1.149 rillig 1833: if (n == 0 && sp->sou_size_in_bits != 0) {
1.78 rillig 1834: /* %s has no named members */
1835: warning(65, t == STRUCT ? "structure" : "union");
1.1 cgd 1836: }
1.76 rillig 1837: return tp;
1.1 cgd 1838: }
1839:
1.78 rillig 1840: type_t *
1841: complete_tag_enum(type_t *tp, sym_t *fmem)
1842: {
1843:
1.124 rillig 1844: setcomplete(tp, true);
1.135 rillig 1845: tp->t_enum->en_first_enumerator = fmem;
1.78 rillig 1846: return tp;
1847: }
1848:
1.1 cgd 1849: /*
1.73 rillig 1850: * Processes the name of an enumerator in an enum declaration.
1.1 cgd 1851: *
1852: * sym points to the enumerator
1853: * val is the value of the enumerator
1.122 rillig 1854: * impl is true if the value of the enumerator was not explicitly specified.
1.1 cgd 1855: */
1856: sym_t *
1.122 rillig 1857: enumeration_constant(sym_t *sym, int val, bool impl)
1.1 cgd 1858: {
1.22 lukem 1859:
1.122 rillig 1860: if (sym->s_scl != NOSCL) {
1.144 rillig 1861: if (sym->s_block_level == block_level) {
1.1 cgd 1862: /* no hflag, because this is illegal!!! */
1863: if (sym->s_arg) {
1864: /* enumeration constant hides parameter: %s */
1865: warning(57, sym->s_name);
1866: } else {
1867: /* redeclaration of %s */
1868: error(27, sym->s_name);
1869: /*
1.73 rillig 1870: * inside blocks it should not be too
1871: * complicated to find the position of the
1872: * previous declaration
1.1 cgd 1873: */
1.144 rillig 1874: if (block_level == 0)
1.75 rillig 1875: print_previous_declaration(-1, sym);
1.1 cgd 1876: }
1877: } else {
1878: if (hflag)
1879: /* redefinition hides earlier one: %s */
1880: warning(43, sym->s_name);
1881: }
1882: sym = pushdown(sym);
1883: }
1.123 rillig 1884: sym->s_scl = CTCONST;
1.3 jpo 1885: sym->s_type = dcs->d_tagtyp;
1.1 cgd 1886: sym->s_value.v_tspec = INT;
1887: sym->s_value.v_quad = val;
1.54 christos 1888: if (impl && val - 1 == TARG_INT_MAX) {
1.1 cgd 1889: /* overflow in enumeration values: %s */
1890: warning(48, sym->s_name);
1891: }
1892: enumval = val + 1;
1.76 rillig 1893: return sym;
1.1 cgd 1894: }
1895:
1896: /*
1897: * Process a single external declarator.
1898: */
1.165 rillig 1899: static void
1.166 rillig 1900: declare_extern(sym_t *dsym, bool initflg, sbuf_t *renaming)
1.1 cgd 1901: {
1.122 rillig 1902: bool dowarn, rval, redec;
1.1 cgd 1903: sym_t *rdsym;
1.166 rillig 1904: char *s;
1905:
1906: if (renaming != NULL) {
1907: lint_assert(dsym->s_rename == NULL);
1908:
1909: s = getlblk(1, renaming->sb_len + 1);
1910: (void)memcpy(s, renaming->sb_name, renaming->sb_len + 1);
1911: dsym->s_rename = s;
1912: }
1.1 cgd 1913:
1.124 rillig 1914: check_function_definition(dsym, true);
1.1 cgd 1915:
1.75 rillig 1916: check_type(dsym);
1.1 cgd 1917:
1.167 rillig 1918: if (initflg && !check_init(dsym))
1.1 cgd 1919: dsym->s_def = DEF;
1920:
1921: /*
1.75 rillig 1922: * Declarations of functions are marked as "tentative" in
1923: * declarator_name(). This is wrong because there are no
1924: * tentative function definitions.
1.1 cgd 1925: */
1926: if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1927: dsym->s_def = DECL;
1928:
1.4 jpo 1929: if (dcs->d_inline) {
1930: if (dsym->s_type->t_tspec == FUNC) {
1.122 rillig 1931: dsym->s_inline = true;
1.4 jpo 1932: } else {
1933: /* variable declared inline: %s */
1.5 jpo 1934: warning(268, dsym->s_name);
1.4 jpo 1935: }
1936: }
1937:
1.1 cgd 1938: /* Write the declaration into the output file */
1939: if (plibflg && llibflg &&
1940: dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1941: /*
1.111 rillig 1942: * With both LINTLIBRARY and PROTOLIB the prototype is
1.1 cgd 1943: * written as a function definition to the output file.
1944: */
1945: rval = dsym->s_type->t_subt->t_tspec != VOID;
1.124 rillig 1946: outfdef(dsym, &dsym->s_def_pos, rval, false, NULL);
1.1 cgd 1947: } else {
1.4 jpo 1948: outsym(dsym, dsym->s_scl, dsym->s_def);
1.1 cgd 1949: }
1950:
1.154 rillig 1951: if ((rdsym = dcs->d_redeclared_symbol) != NULL) {
1.1 cgd 1952:
1953: /*
1.73 rillig 1954: * If the old symbol stems from an old style function
1955: * definition, we have remembered the params in rdsmy->s_args
1956: * and compare them with the params of the prototype.
1.1 cgd 1957: */
1.6 jpo 1958: if (rdsym->s_osdef && dsym->s_type->t_proto) {
1.75 rillig 1959: redec = check_old_style_definition(rdsym, dsym);
1.6 jpo 1960: } else {
1.122 rillig 1961: redec = false;
1.6 jpo 1962: }
1.1 cgd 1963:
1.122 rillig 1964: if (!redec &&
1965: !check_redeclaration(dsym, (dowarn = false, &dowarn))) {
1.22 lukem 1966:
1.44 dholland 1967: if (dowarn) {
1.105 rillig 1968: if (sflag)
1969: /* redeclaration of %s */
1970: error(27, dsym->s_name);
1971: else
1972: /* redeclaration of %s */
1973: warning(27, dsym->s_name);
1.75 rillig 1974: print_previous_declaration(-1, rdsym);
1.1 cgd 1975: }
1976:
1977: /*
1.73 rillig 1978: * Take over the remembered params if the new symbol
1.4 jpo 1979: * is not a prototype.
1.1 cgd 1980: */
1981: if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1982: dsym->s_osdef = rdsym->s_osdef;
1983: dsym->s_args = rdsym->s_args;
1.85 rillig 1984: dsym->s_def_pos = rdsym->s_def_pos;
1.1 cgd 1985: }
1986:
1987: /*
1988: * Remember the position of the declaration if the
1.4 jpo 1989: * old symbol was a prototype and the new is not.
1990: * Also remember the position if the old symbol
1.1 cgd 1991: * was defined and the new is not.
1992: */
1993: if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1.85 rillig 1994: dsym->s_def_pos = rdsym->s_def_pos;
1.1 cgd 1995: } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1.85 rillig 1996: dsym->s_def_pos = rdsym->s_def_pos;
1.1 cgd 1997: }
1998:
1999: /*
1.79 rillig 2000: * Copy usage information of the name into the new
2001: * symbol.
1.1 cgd 2002: */
1.75 rillig 2003: copy_usage_info(dsym, rdsym);
1.1 cgd 2004:
2005: /* Once a name is defined, it remains defined. */
2006: if (rdsym->s_def == DEF)
2007: dsym->s_def = DEF;
2008:
1.4 jpo 2009: /* once a function is inline, it remains inline */
2010: if (rdsym->s_inline)
1.122 rillig 2011: dsym->s_inline = true;
1.4 jpo 2012:
1.75 rillig 2013: complete_type(dsym, rdsym);
1.1 cgd 2014:
2015: }
1.22 lukem 2016:
1.1 cgd 2017: rmsym(rdsym);
2018: }
2019:
2020: if (dsym->s_scl == TYPEDEF) {
1.169 rillig 2021: dsym->s_type = dup_type(dsym->s_type);
1.122 rillig 2022: dsym->s_type->t_typedef = true;
1.1 cgd 2023: settdsym(dsym->s_type, dsym);
2024: }
2025:
2026: }
2027:
1.165 rillig 2028: void
2029: declare(sym_t *decl, bool initflg, sbuf_t *renaming)
2030: {
2031:
1.166 rillig 2032: if (dcs->d_ctx == EXTERN) {
2033: declare_extern(decl, initflg, renaming);
2034: } else if (dcs->d_ctx == ARG) {
1.165 rillig 2035: if (renaming != NULL) {
2036: /* symbol renaming can't be used on function arguments */
2037: error(310);
1.166 rillig 2038: } else
2039: (void)declare_argument(decl, initflg);
2040: } else {
1.165 rillig 2041: lint_assert(dcs->d_ctx == AUTO);
2042: if (renaming != NULL) {
2043: /* symbol renaming can't be used on automatic variables */
2044: error(311);
1.166 rillig 2045: } else
2046: declare_local(decl, initflg);
1.165 rillig 2047: }
2048: }
2049:
1.1 cgd 2050: /*
1.73 rillig 2051: * Copies information about usage into a new symbol table entry of
1.1 cgd 2052: * the same symbol.
2053: */
2054: void
1.75 rillig 2055: copy_usage_info(sym_t *sym, sym_t *rdsym)
1.1 cgd 2056: {
1.22 lukem 2057:
1.84 rillig 2058: sym->s_set_pos = rdsym->s_set_pos;
2059: sym->s_use_pos = rdsym->s_use_pos;
1.1 cgd 2060: sym->s_set = rdsym->s_set;
2061: sym->s_used = rdsym->s_used;
2062: }
2063:
2064: /*
1.131 rillig 2065: * Prints an error and returns true if a symbol is redeclared/redefined.
2066: * Otherwise returns false and, in some cases of minor problems, prints
1.1 cgd 2067: * a warning.
2068: */
1.122 rillig 2069: bool
2070: check_redeclaration(sym_t *dsym, bool *dowarn)
1.1 cgd 2071: {
2072: sym_t *rsym;
2073:
1.154 rillig 2074: if ((rsym = dcs->d_redeclared_symbol)->s_scl == CTCONST) {
1.1 cgd 2075: /* redeclaration of %s */
2076: error(27, dsym->s_name);
1.75 rillig 2077: print_previous_declaration(-1, rsym);
1.122 rillig 2078: return true;
1.1 cgd 2079: }
2080: if (rsym->s_scl == TYPEDEF) {
2081: /* typedef redeclared: %s */
2082: error(89, dsym->s_name);
1.75 rillig 2083: print_previous_declaration(-1, rsym);
1.122 rillig 2084: return true;
1.1 cgd 2085: }
2086: if (dsym->s_scl == TYPEDEF) {
2087: /* redeclaration of %s */
2088: error(27, dsym->s_name);
1.75 rillig 2089: print_previous_declaration(-1, rsym);
1.122 rillig 2090: return true;
1.1 cgd 2091: }
2092: if (rsym->s_def == DEF && dsym->s_def == DEF) {
2093: /* redefinition of %s */
2094: error(28, dsym->s_name);
1.75 rillig 2095: print_previous_declaration(-1, rsym);
1.122 rillig 2096: return true;
1.1 cgd 2097: }
1.124 rillig 2098: if (!eqtype(rsym->s_type, dsym->s_type, false, false, dowarn)) {
1.1 cgd 2099: /* redeclaration of %s */
2100: error(27, dsym->s_name);
1.75 rillig 2101: print_previous_declaration(-1, rsym);
1.122 rillig 2102: return true;
1.1 cgd 2103: }
2104: if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
1.122 rillig 2105: return false;
1.1 cgd 2106: if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
1.122 rillig 2107: return false;
1.1 cgd 2108: if (rsym->s_scl == STATIC && dsym->s_def == DECL)
1.122 rillig 2109: return false;
1.1 cgd 2110: if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
2111: /*
1.66 kamil 2112: * All cases except "int a = 1; static int a;" are caught
1.1 cgd 2113: * above with or without a warning
2114: */
2115: /* redeclaration of %s */
2116: error(27, dsym->s_name);
1.75 rillig 2117: print_previous_declaration(-1, rsym);
1.122 rillig 2118: return true;
1.1 cgd 2119: }
2120: if (rsym->s_scl == EXTERN) {
2121: /* previously declared extern, becomes static: %s */
2122: warning(29, dsym->s_name);
1.75 rillig 2123: print_previous_declaration(-1, rsym);
1.122 rillig 2124: return false;
1.1 cgd 2125: }
2126: /*
1.86 rillig 2127: * Now it's one of:
1.1 cgd 2128: * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2129: */
2130: /* redeclaration of %s; ANSI C requires "static" */
2131: if (sflag) {
1.94 rillig 2132: /* redeclaration of %s; ANSI C requires static */
1.1 cgd 2133: warning(30, dsym->s_name);
1.75 rillig 2134: print_previous_declaration(-1, rsym);
1.1 cgd 2135: }
2136: dsym->s_scl = STATIC;
1.122 rillig 2137: return false;
1.1 cgd 2138: }
2139:
1.117 rillig 2140: static bool
1.122 rillig 2141: qualifiers_correspond(const type_t *tp1, const type_t *tp2, bool ignqual)
1.45 christos 2142: {
2143: if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
1.117 rillig 2144: return false;
1.45 christos 2145:
2146: if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
1.117 rillig 2147: return false;
1.45 christos 2148:
1.117 rillig 2149: return true;
1.45 christos 2150: }
2151:
1.117 rillig 2152: bool
1.122 rillig 2153: eqptrtype(const type_t *tp1, const type_t *tp2, bool ignqual)
1.45 christos 2154: {
2155: if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
1.117 rillig 2156: return false;
1.45 christos 2157:
1.116 rillig 2158: if (!qualifiers_correspond(tp1, tp2, ignqual))
1.117 rillig 2159: return false;
1.45 christos 2160:
1.117 rillig 2161: return true;
1.45 christos 2162: }
2163:
2164:
1.1 cgd 2165: /*
1.131 rillig 2166: * Checks if two types are compatible.
1.1 cgd 2167: *
2168: * ignqual ignore qualifiers of type; used for function params
1.15 mycroft 2169: * promot promote left type; used for comparison of params of
1.1 cgd 2170: * old style function definitions with params of prototypes.
1.131 rillig 2171: * *dowarn set to true if an old style function declaration is not
1.1 cgd 2172: * compatible with a prototype
2173: */
1.122 rillig 2174: bool
1.121 rillig 2175: eqtype(const type_t *tp1, const type_t *tp2,
1.122 rillig 2176: bool ignqual, bool promot, bool *dowarn)
1.1 cgd 2177: {
2178: tspec_t t;
2179:
2180: while (tp1 != NULL && tp2 != NULL) {
2181:
2182: t = tp1->t_tspec;
2183: if (promot) {
2184: if (t == FLOAT) {
2185: t = DOUBLE;
2186: } else if (t == CHAR || t == SCHAR) {
2187: t = INT;
2188: } else if (t == UCHAR) {
2189: t = tflag ? UINT : INT;
2190: } else if (t == SHORT) {
2191: t = INT;
2192: } else if (t == USHORT) {
2193: /* CONSTCOND */
1.54 christos 2194: t = TARG_INT_MAX < TARG_USHRT_MAX || tflag ? UINT : INT;
1.1 cgd 2195: }
2196: }
2197:
2198: if (t != tp2->t_tspec)
1.124 rillig 2199: return false;
1.1 cgd 2200:
1.116 rillig 2201: if (!qualifiers_correspond(tp1, tp2, ignqual))
1.124 rillig 2202: return false;
1.1 cgd 2203:
2204: if (t == STRUCT || t == UNION)
1.76 rillig 2205: return tp1->t_str == tp2->t_str;
1.1 cgd 2206:
2207: if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2208: if (tp1->t_dim != 0 && tp2->t_dim != 0)
1.124 rillig 2209: return false;
1.1 cgd 2210: }
2211:
2212: /* dont check prototypes for traditional */
2213: if (t == FUNC && !tflag) {
2214: if (tp1->t_proto && tp2->t_proto) {
1.44 dholland 2215: if (!eqargs(tp1, tp2, dowarn))
1.124 rillig 2216: return false;
1.1 cgd 2217: } else if (tp1->t_proto) {
1.44 dholland 2218: if (!mnoarg(tp1, dowarn))
1.124 rillig 2219: return false;
1.1 cgd 2220: } else if (tp2->t_proto) {
1.44 dholland 2221: if (!mnoarg(tp2, dowarn))
1.124 rillig 2222: return false;
1.1 cgd 2223: }
2224: }
2225:
2226: tp1 = tp1->t_subt;
2227: tp2 = tp2->t_subt;
1.122 rillig 2228: ignqual = promot = false;
1.1 cgd 2229:
2230: }
2231:
1.76 rillig 2232: return tp1 == tp2;
1.1 cgd 2233: }
2234:
2235: /*
1.6 jpo 2236: * Compares the parameter types of two prototypes.
1.1 cgd 2237: */
1.122 rillig 2238: static bool
2239: eqargs(const type_t *tp1, const type_t *tp2, bool *dowarn)
1.1 cgd 2240: {
2241: sym_t *a1, *a2;
2242:
2243: if (tp1->t_vararg != tp2->t_vararg)
1.124 rillig 2244: return false;
1.1 cgd 2245:
2246: a1 = tp1->t_args;
2247: a2 = tp2->t_args;
2248:
2249: while (a1 != NULL && a2 != NULL) {
2250:
1.125 rillig 2251: if (!eqtype(a1->s_type, a2->s_type, true, false, dowarn))
1.124 rillig 2252: return false;
1.1 cgd 2253:
1.80 rillig 2254: a1 = a1->s_next;
2255: a2 = a2->s_next;
1.1 cgd 2256:
2257: }
2258:
1.76 rillig 2259: return a1 == a2;
1.1 cgd 2260: }
2261:
2262: /*
2263: * mnoarg() (matches functions with no argument type information)
1.130 rillig 2264: * returns whether all parameters of a prototype are compatible with
1.73 rillig 2265: * an old style function declaration.
2266: * This is the case if the following conditions are met:
1.130 rillig 2267: * 1. the prototype has a fixed number of parameters
1.1 cgd 2268: * 2. no parameter is of type float
2269: * 3. no parameter is converted to another type if integer promotion
2270: * is applied on it
2271: */
1.122 rillig 2272: static bool
2273: mnoarg(const type_t *tp, bool *dowarn)
1.1 cgd 2274: {
2275: sym_t *arg;
2276: tspec_t t;
2277:
2278: if (tp->t_vararg) {
1.44 dholland 2279: if (dowarn != NULL)
1.122 rillig 2280: *dowarn = true;
1.1 cgd 2281: }
1.80 rillig 2282: for (arg = tp->t_args; arg != NULL; arg = arg->s_next) {
1.1 cgd 2283: if ((t = arg->s_type->t_tspec) == FLOAT ||
2284: t == CHAR || t == SCHAR || t == UCHAR ||
2285: t == SHORT || t == USHORT) {
1.44 dholland 2286: if (dowarn != NULL)
1.122 rillig 2287: *dowarn = true;
1.1 cgd 2288: }
2289: }
1.122 rillig 2290: return true;
1.1 cgd 2291: }
2292:
2293: /*
2294: * Compares a prototype declaration with the remembered arguments of
2295: * a previous old style function definition.
2296: */
1.122 rillig 2297: static bool
1.75 rillig 2298: check_old_style_definition(sym_t *rdsym, sym_t *dsym)
1.1 cgd 2299: {
2300: sym_t *args, *pargs, *arg, *parg;
2301: int narg, nparg, n;
1.122 rillig 2302: bool dowarn, msg;
1.1 cgd 2303:
2304: args = rdsym->s_args;
2305: pargs = dsym->s_type->t_args;
2306:
1.122 rillig 2307: msg = false;
1.1 cgd 2308:
2309: narg = nparg = 0;
1.80 rillig 2310: for (arg = args; arg != NULL; arg = arg->s_next)
1.1 cgd 2311: narg++;
1.80 rillig 2312: for (parg = pargs; parg != NULL; parg = parg->s_next)
1.1 cgd 2313: nparg++;
2314: if (narg != nparg) {
2315: /* prototype does not match old-style definition */
2316: error(63);
1.122 rillig 2317: msg = true;
1.1 cgd 2318: goto end;
2319: }
2320:
2321: arg = args;
2322: parg = pargs;
2323: n = 1;
1.122 rillig 2324: while (narg-- > 0) {
2325: dowarn = false;
1.1 cgd 2326: /*
2327: * If it does not match due to promotion and sflag is
2328: * not set we print only a warning.
2329: */
1.124 rillig 2330: if (!eqtype(arg->s_type, parg->s_type, true, true, &dowarn) ||
2331: dowarn) {
1.88 rillig 2332: /* prototype does not match old style defn., arg #%d */
1.1 cgd 2333: error(299, n);
1.122 rillig 2334: msg = true;
1.1 cgd 2335: }
1.80 rillig 2336: arg = arg->s_next;
2337: parg = parg->s_next;
1.1 cgd 2338: n++;
2339: }
2340:
2341: end:
1.6 jpo 2342: if (msg)
1.7 jpo 2343: /* old style definition */
1.75 rillig 2344: print_previous_declaration(300, rdsym);
1.6 jpo 2345:
1.76 rillig 2346: return msg;
1.1 cgd 2347: }
2348:
2349: /*
1.73 rillig 2350: * Completes a type by copying the dimension and prototype information
1.1 cgd 2351: * from a second compatible type.
2352: *
2353: * Following lines are legal:
2354: * "typedef a[]; a b; a b[10]; a c; a c[20];"
2355: * "typedef ft(); ft f; f(int); ft g; g(long);"
2356: * This means that, if a type is completed, the type structure must
2357: * be duplicated.
2358: */
2359: void
1.75 rillig 2360: complete_type(sym_t *dsym, sym_t *ssym)
1.1 cgd 2361: {
2362: type_t **dstp, *src;
2363: type_t *dst;
2364:
2365: dstp = &dsym->s_type;
2366: src = ssym->s_type;
2367:
2368: while ((dst = *dstp) != NULL) {
1.92 rillig 2369: lint_assert(src != NULL);
2370: lint_assert(dst->t_tspec == src->t_tspec);
1.1 cgd 2371: if (dst->t_tspec == ARRAY) {
2372: if (dst->t_dim == 0 && src->t_dim != 0) {
1.169 rillig 2373: *dstp = dst = dup_type(dst);
1.1 cgd 2374: dst->t_dim = src->t_dim;
1.124 rillig 2375: setcomplete(dst, true);
1.1 cgd 2376: }
2377: } else if (dst->t_tspec == FUNC) {
2378: if (!dst->t_proto && src->t_proto) {
1.169 rillig 2379: *dstp = dst = dup_type(dst);
1.122 rillig 2380: dst->t_proto = true;
1.1 cgd 2381: dst->t_args = src->t_args;
2382: }
2383: }
2384: dstp = &dst->t_subt;
2385: src = src->t_subt;
2386: }
2387: }
2388:
2389: /*
2390: * Completes the declaration of a single argument.
2391: */
2392: sym_t *
1.122 rillig 2393: declare_argument(sym_t *sym, bool initflg)
1.1 cgd 2394: {
2395: tspec_t t;
2396:
1.124 rillig 2397: check_function_definition(sym, true);
1.1 cgd 2398:
1.75 rillig 2399: check_type(sym);
1.1 cgd 2400:
1.154 rillig 2401: if (dcs->d_redeclared_symbol != NULL &&
2402: dcs->d_redeclared_symbol->s_block_level == block_level) {
1.1 cgd 2403: /* redeclaration of formal parameter %s */
2404: error(237, sym->s_name);
1.154 rillig 2405: rmsym(dcs->d_redeclared_symbol);
1.122 rillig 2406: sym->s_arg = true;
1.1 cgd 2407: }
2408:
2409: if (!sym->s_arg) {
2410: /* declared argument %s is missing */
2411: error(53, sym->s_name);
1.122 rillig 2412: sym->s_arg = true;
1.1 cgd 2413: }
2414:
2415: if (initflg) {
2416: /* cannot initialize parameter: %s */
2417: error(52, sym->s_name);
2418: }
2419:
2420: if ((t = sym->s_type->t_tspec) == ARRAY) {
1.168 rillig 2421: sym->s_type = derive_type(sym->s_type->t_subt, PTR);
1.1 cgd 2422: } else if (t == FUNC) {
2423: if (tflag)
2424: /* a function is declared as an argument: %s */
2425: warning(50, sym->s_name);
1.168 rillig 2426: sym->s_type = derive_type(sym->s_type, PTR);
1.1 cgd 2427: } else if (t == FLOAT) {
2428: if (tflag)
2429: sym->s_type = gettyp(DOUBLE);
2430: }
2431:
1.4 jpo 2432: if (dcs->d_inline)
2433: /* argument declared inline: %s */
1.5 jpo 2434: warning(269, sym->s_name);
1.4 jpo 2435:
1.1 cgd 2436: /*
2437: * Arguments must have complete types. lengths() prints the needed
2438: * error messages (null dimension is impossible because arrays are
2439: * converted to pointers).
2440: */
2441: if (sym->s_type->t_tspec != VOID)
2442: (void)length(sym->s_type, sym->s_name);
2443:
1.67 christos 2444: sym->s_used = dcs->d_used;
1.75 rillig 2445: mark_as_set(sym);
1.1 cgd 2446:
1.76 rillig 2447: return sym;
1.1 cgd 2448: }
2449:
2450: void
1.87 rillig 2451: check_func_lint_directives(void)
1.1 cgd 2452: {
1.87 rillig 2453: sym_t *arg;
2454: int narg, n;
2455: tspec_t t;
1.1 cgd 2456:
2457: /* check for illegal combinations of lint directives */
1.110 rillig 2458: if (printflike_argnum != -1 && scanflike_argnum != -1) {
1.1 cgd 2459: /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2460: warning(289);
1.110 rillig 2461: printflike_argnum = scanflike_argnum = -1;
1.1 cgd 2462: }
1.110 rillig 2463: if (nvararg != -1 &&
2464: (printflike_argnum != -1 || scanflike_argnum != -1)) {
1.1 cgd 2465: /* dubious use of ** VARARGS ** with ** %s ** */
1.110 rillig 2466: warning(288,
2467: printflike_argnum != -1 ? "PRINTFLIKE" : "SCANFLIKE");
1.1 cgd 2468: nvararg = -1;
2469: }
2470:
2471: /*
2472: * check if the argument of a lint directive is compatible with the
2473: * number of arguments.
2474: */
2475: narg = 0;
1.156 rillig 2476: for (arg = dcs->d_func_args; arg != NULL; arg = arg->s_next)
1.1 cgd 2477: narg++;
2478: if (nargusg > narg) {
2479: /* argument number mismatch with directive: ** %s ** */
2480: warning(283, "ARGSUSED");
2481: nargusg = 0;
2482: }
2483: if (nvararg > narg) {
2484: /* argument number mismatch with directive: ** %s ** */
2485: warning(283, "VARARGS");
2486: nvararg = 0;
2487: }
1.110 rillig 2488: if (printflike_argnum > narg) {
1.1 cgd 2489: /* argument number mismatch with directive: ** %s ** */
2490: warning(283, "PRINTFLIKE");
1.110 rillig 2491: printflike_argnum = -1;
2492: } else if (printflike_argnum == 0) {
2493: printflike_argnum = -1;
1.1 cgd 2494: }
1.110 rillig 2495: if (scanflike_argnum > narg) {
1.1 cgd 2496: /* argument number mismatch with directive: ** %s ** */
2497: warning(283, "SCANFLIKE");
1.110 rillig 2498: scanflike_argnum = -1;
2499: } else if (scanflike_argnum == 0) {
2500: scanflike_argnum = -1;
2501: }
2502: if (printflike_argnum != -1 || scanflike_argnum != -1) {
2503: narg = printflike_argnum != -1
2504: ? printflike_argnum : scanflike_argnum;
1.156 rillig 2505: arg = dcs->d_func_args;
1.6 jpo 2506: for (n = 1; n < narg; n++)
1.80 rillig 2507: arg = arg->s_next;
1.1 cgd 2508: if (arg->s_type->t_tspec != PTR ||
2509: ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2510: t != UCHAR && t != SCHAR)) {
2511: /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2512: warning(293, narg);
1.110 rillig 2513: printflike_argnum = scanflike_argnum = -1;
1.1 cgd 2514: }
2515: }
1.87 rillig 2516: }
2517:
2518: /*
2519: * Warn about arguments in old style function definitions that default to int.
2520: * Check that an old style function definition is compatible to a previous
2521: * prototype.
2522: */
2523: void
2524: check_func_old_style_arguments(void)
2525: {
2526: sym_t *args, *arg, *pargs, *parg;
1.122 rillig 2527: int narg, nparg;
2528: bool msg;
1.87 rillig 2529:
2530: args = funcsym->s_args;
2531: pargs = funcsym->s_type->t_args;
1.1 cgd 2532:
2533: /*
1.73 rillig 2534: * print a warning for each argument of an old style function
1.1 cgd 2535: * definition which defaults to int
2536: */
1.80 rillig 2537: for (arg = args; arg != NULL; arg = arg->s_next) {
1.1 cgd 2538: if (arg->s_defarg) {
1.88 rillig 2539: /* argument type defaults to 'int': %s */
1.1 cgd 2540: warning(32, arg->s_name);
1.122 rillig 2541: arg->s_defarg = false;
1.75 rillig 2542: mark_as_set(arg);
1.1 cgd 2543: }
2544: }
2545:
2546: /*
1.73 rillig 2547: * If this is an old style function definition and a prototype
1.1 cgd 2548: * exists, compare the types of arguments.
2549: */
2550: if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2551: /*
1.73 rillig 2552: * If the number of arguments does not match, we need not
1.1 cgd 2553: * continue.
2554: */
2555: narg = nparg = 0;
1.122 rillig 2556: msg = false;
1.80 rillig 2557: for (parg = pargs; parg != NULL; parg = parg->s_next)
1.1 cgd 2558: nparg++;
1.80 rillig 2559: for (arg = args; arg != NULL; arg = arg->s_next)
1.1 cgd 2560: narg++;
2561: if (narg != nparg) {
2562: /* parameter mismatch: %d declared, %d defined */
2563: error(51, nparg, narg);
1.122 rillig 2564: msg = true;
1.1 cgd 2565: } else {
2566: parg = pargs;
2567: arg = args;
1.122 rillig 2568: while (narg-- > 0) {
1.75 rillig 2569: msg |= check_prototype_declaration(arg, parg);
1.80 rillig 2570: parg = parg->s_next;
2571: arg = arg->s_next;
1.1 cgd 2572: }
2573: }
1.6 jpo 2574: if (msg)
1.1 cgd 2575: /* prototype declaration */
1.154 rillig 2576: print_previous_declaration(285,
2577: dcs->d_redeclared_symbol);
1.6 jpo 2578:
1.73 rillig 2579: /* from now on the prototype is valid */
1.122 rillig 2580: funcsym->s_osdef = false;
1.6 jpo 2581: funcsym->s_args = NULL;
1.1 cgd 2582: }
2583: }
2584:
2585: /*
2586: * Checks compatibility of an old style function definition with a previous
2587: * prototype declaration.
1.122 rillig 2588: * Returns true if the position of the previous declaration should be reported.
1.1 cgd 2589: */
1.122 rillig 2590: static bool
1.75 rillig 2591: check_prototype_declaration(sym_t *arg, sym_t *parg)
1.1 cgd 2592: {
2593: type_t *tp, *ptp;
1.122 rillig 2594: bool dowarn, msg;
1.1 cgd 2595:
2596: tp = arg->s_type;
2597: ptp = parg->s_type;
2598:
1.122 rillig 2599: msg = false;
2600: dowarn = false;
1.6 jpo 2601:
1.124 rillig 2602: if (!eqtype(tp, ptp, true, true, &dowarn)) {
2603: if (eqtype(tp, ptp, true, false, &dowarn)) {
1.6 jpo 2604: /* type does not match prototype: %s */
1.106 rillig 2605: gnuism(58, arg->s_name);
2606: msg = sflag || !gflag;
1.6 jpo 2607: } else {
2608: /* type does not match prototype: %s */
2609: error(58, arg->s_name);
1.122 rillig 2610: msg = true;
1.6 jpo 2611: }
1.44 dholland 2612: } else if (dowarn) {
1.105 rillig 2613: if (sflag)
2614: /* type does not match prototype: %s */
2615: error(58, arg->s_name);
2616: else
2617: /* type does not match prototype: %s */
2618: warning(58, arg->s_name);
1.122 rillig 2619: msg = true;
1.1 cgd 2620: }
1.6 jpo 2621:
1.76 rillig 2622: return msg;
1.1 cgd 2623: }
2624:
2625: /*
2626: * Completes a single local declaration/definition.
2627: */
2628: void
1.122 rillig 2629: declare_local(sym_t *dsym, bool initflg)
1.1 cgd 2630: {
1.22 lukem 2631:
1.75 rillig 2632: /* Correct a mistake done in declarator_name(). */
1.1 cgd 2633: if (dsym->s_type->t_tspec == FUNC) {
2634: dsym->s_def = DECL;
1.3 jpo 2635: if (dcs->d_scl == NOSCL)
1.1 cgd 2636: dsym->s_scl = EXTERN;
2637: }
2638:
2639: if (dsym->s_type->t_tspec == FUNC) {
2640: if (dsym->s_scl == STATIC) {
2641: /* dubious static function at block level: %s */
2642: warning(93, dsym->s_name);
2643: dsym->s_scl = EXTERN;
2644: } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2645: /* function has illegal storage class: %s */
2646: error(94, dsym->s_name);
2647: dsym->s_scl = EXTERN;
2648: }
2649: }
2650:
1.4 jpo 2651: /*
2652: * functions may be declared inline at local scope, although
2653: * this has no effect for a later definition of the same
2654: * function.
2655: * XXX it should have an effect if tflag is set. this would
2656: * also be the way gcc behaves.
2657: */
2658: if (dcs->d_inline) {
2659: if (dsym->s_type->t_tspec == FUNC) {
1.122 rillig 2660: dsym->s_inline = true;
1.4 jpo 2661: } else {
2662: /* variable declared inline: %s */
1.5 jpo 2663: warning(268, dsym->s_name);
1.4 jpo 2664: }
2665: }
2666:
1.124 rillig 2667: check_function_definition(dsym, true);
1.1 cgd 2668:
1.75 rillig 2669: check_type(dsym);
1.1 cgd 2670:
1.154 rillig 2671: if (dcs->d_redeclared_symbol != NULL && dsym->s_scl == EXTERN)
1.116 rillig 2672: declare_external_in_block(dsym);
1.1 cgd 2673:
2674: if (dsym->s_scl == EXTERN) {
2675: /*
1.73 rillig 2676: * XXX if the static variable at level 0 is only defined
2677: * later, checking will be possible.
1.1 cgd 2678: */
1.83 rillig 2679: if (dsym->s_ext_sym == NULL) {
1.4 jpo 2680: outsym(dsym, EXTERN, dsym->s_def);
1.1 cgd 2681: } else {
1.83 rillig 2682: outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
1.1 cgd 2683: }
2684: }
2685:
1.154 rillig 2686: if (dcs->d_redeclared_symbol != NULL) {
1.1 cgd 2687:
1.154 rillig 2688: if (dcs->d_redeclared_symbol->s_block_level == 0) {
1.1 cgd 2689:
2690: switch (dsym->s_scl) {
2691: case AUTO:
2692: if (hflag)
1.94 rillig 2693: /* automatic hides external decl.: %s */
1.1 cgd 2694: warning(86, dsym->s_name);
2695: break;
2696: case STATIC:
2697: if (hflag)
1.94 rillig 2698: /* static hides external decl.: %s */
1.1 cgd 2699: warning(87, dsym->s_name);
2700: break;
2701: case TYPEDEF:
2702: if (hflag)
1.94 rillig 2703: /* typedef hides external decl.: %s */
1.1 cgd 2704: warning(88, dsym->s_name);
2705: break;
2706: case EXTERN:
2707: /*
1.116 rillig 2708: * Warnings and errors are printed in
2709: * declare_external_in_block()
1.1 cgd 2710: */
2711: break;
2712: default:
1.124 rillig 2713: lint_assert(/*CONSTCOND*/false);
1.1 cgd 2714: }
2715:
1.154 rillig 2716: } else if (dcs->d_redeclared_symbol->s_block_level ==
2717: block_level) {
1.1 cgd 2718:
1.86 rillig 2719: /* no hflag, because it's illegal! */
1.154 rillig 2720: if (dcs->d_redeclared_symbol->s_arg) {
1.7 jpo 2721: /*
2722: * if !tflag, a "redeclaration of %s" error
2723: * is produced below
2724: */
2725: if (tflag) {
2726: if (hflag)
2727: /* decl. hides parameter: %s */
2728: warning(91, dsym->s_name);
1.154 rillig 2729: rmsym(dcs->d_redeclared_symbol);
1.7 jpo 2730: }
1.1 cgd 2731: }
2732:
1.154 rillig 2733: } else if (dcs->d_redeclared_symbol->s_block_level <
2734: block_level) {
1.1 cgd 2735:
2736: if (hflag)
2737: /* declaration hides earlier one: %s */
2738: warning(95, dsym->s_name);
1.22 lukem 2739:
1.1 cgd 2740: }
2741:
1.154 rillig 2742: if (dcs->d_redeclared_symbol->s_block_level == block_level) {
1.1 cgd 2743:
2744: /* redeclaration of %s */
2745: error(27, dsym->s_name);
1.154 rillig 2746: rmsym(dcs->d_redeclared_symbol);
1.1 cgd 2747:
2748: }
2749:
2750: }
2751:
1.167 rillig 2752: if (initflg && !check_init(dsym)) {
1.1 cgd 2753: dsym->s_def = DEF;
1.75 rillig 2754: mark_as_set(dsym);
1.1 cgd 2755: }
2756:
2757: if (dsym->s_scl == TYPEDEF) {
1.169 rillig 2758: dsym->s_type = dup_type(dsym->s_type);
1.122 rillig 2759: dsym->s_type->t_typedef = true;
1.1 cgd 2760: settdsym(dsym->s_type, dsym);
2761: }
2762:
2763: /*
1.73 rillig 2764: * Before we can check the size we must wait for a initialization
1.1 cgd 2765: * which may follow.
2766: */
2767: }
2768:
2769: /*
1.73 rillig 2770: * Processes (re)declarations of external symbols inside blocks.
1.1 cgd 2771: */
2772: static void
1.116 rillig 2773: declare_external_in_block(sym_t *dsym)
1.1 cgd 2774: {
1.122 rillig 2775: bool eqt, dowarn;
1.1 cgd 2776: sym_t *esym;
2777:
2778: /* look for a symbol with the same name */
1.154 rillig 2779: esym = dcs->d_redeclared_symbol;
1.144 rillig 2780: while (esym != NULL && esym->s_block_level != 0) {
1.1 cgd 2781: while ((esym = esym->s_link) != NULL) {
2782: if (esym->s_kind != FVFT)
2783: continue;
2784: if (strcmp(dsym->s_name, esym->s_name) == 0)
2785: break;
2786: }
2787: }
2788: if (esym == NULL)
2789: return;
2790: if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2791: /* gcc accepts this without a warning, pcc prints an error. */
2792: /* redeclaration of %s */
2793: warning(27, dsym->s_name);
1.75 rillig 2794: print_previous_declaration(-1, esym);
1.1 cgd 2795: return;
2796: }
2797:
1.122 rillig 2798: dowarn = false;
1.124 rillig 2799: eqt = eqtype(esym->s_type, dsym->s_type, false, false, &dowarn);
1.1 cgd 2800:
1.44 dholland 2801: if (!eqt || dowarn) {
1.1 cgd 2802: if (esym->s_scl == EXTERN) {
2803: /* inconsistent redeclaration of extern: %s */
2804: warning(90, dsym->s_name);
1.75 rillig 2805: print_previous_declaration(-1, esym);
1.1 cgd 2806: } else {
2807: /* inconsistent redeclaration of static: %s */
2808: warning(92, dsym->s_name);
1.75 rillig 2809: print_previous_declaration(-1, esym);
1.1 cgd 2810: }
2811: }
2812:
2813: if (eqt) {
2814: /*
2815: * Remember the external symbol so we can update usage
2816: * information at the end of the block.
2817: */
1.83 rillig 2818: dsym->s_ext_sym = esym;
1.1 cgd 2819: }
2820: }
2821:
2822: /*
1.73 rillig 2823: * Print an error or a warning if the symbol cannot be initialized due
1.131 rillig 2824: * to type/storage class. Return whether an error has been detected.
1.1 cgd 2825: */
1.122 rillig 2826: static bool
1.75 rillig 2827: check_init(sym_t *sym)
1.1 cgd 2828: {
1.122 rillig 2829: bool erred;
1.1 cgd 2830:
1.122 rillig 2831: erred = false;
1.1 cgd 2832:
2833: if (sym->s_type->t_tspec == FUNC) {
2834: /* cannot initialize function: %s */
2835: error(24, sym->s_name);
1.122 rillig 2836: erred = true;
1.1 cgd 2837: } else if (sym->s_scl == TYPEDEF) {
2838: /* cannot initialize typedef: %s */
2839: error(25, sym->s_name);
1.122 rillig 2840: erred = true;
1.1 cgd 2841: } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2842: /* cannot initialize "extern" declaration: %s */
1.3 jpo 2843: if (dcs->d_ctx == EXTERN) {
1.94 rillig 2844: /* cannot initialize extern declaration: %s */
1.1 cgd 2845: warning(26, sym->s_name);
2846: } else {
1.94 rillig 2847: /* cannot initialize extern declaration: %s */
1.1 cgd 2848: error(26, sym->s_name);
1.122 rillig 2849: erred = true;
1.1 cgd 2850: }
2851: }
2852:
1.76 rillig 2853: return erred;
1.1 cgd 2854: }
2855:
2856: /*
1.73 rillig 2857: * Create a symbol for an abstract declaration.
1.1 cgd 2858: */
2859: sym_t *
1.75 rillig 2860: abstract_name(void)
1.1 cgd 2861: {
2862: sym_t *sym;
2863:
1.137 rillig 2864: lint_assert(dcs->d_ctx == ABSTRACT || dcs->d_ctx == PROTO_ARG);
1.1 cgd 2865:
1.170 rillig 2866: sym = getblk(sizeof(*sym));
1.1 cgd 2867:
2868: sym->s_name = unnamed;
2869: sym->s_def = DEF;
2870: sym->s_scl = ABSTRACT;
1.144 rillig 2871: sym->s_block_level = -1;
1.1 cgd 2872:
1.137 rillig 2873: if (dcs->d_ctx == PROTO_ARG)
1.122 rillig 2874: sym->s_arg = true;
1.1 cgd 2875:
1.3 jpo 2876: sym->s_type = dcs->d_type;
1.154 rillig 2877: dcs->d_redeclared_symbol = NULL;
1.122 rillig 2878: dcs->d_vararg = false;
1.1 cgd 2879:
1.76 rillig 2880: return sym;
1.1 cgd 2881: }
2882:
2883: /*
2884: * Removes anything which has nothing to do on global level.
2885: */
2886: void
1.77 rillig 2887: global_clean_up(void)
1.1 cgd 2888: {
1.22 lukem 2889:
1.82 rillig 2890: while (dcs->d_next != NULL)
1.160 rillig 2891: end_declaration_level();
1.1 cgd 2892:
2893: cleanup();
1.144 rillig 2894: block_level = 0;
2895: mem_block_level = 0;
1.1 cgd 2896:
2897: /*
1.73 rillig 2898: * remove all information about pending lint directives without
1.1 cgd 2899: * warnings.
2900: */
1.124 rillig 2901: global_clean_up_decl(true);
1.1 cgd 2902: }
2903:
2904: /*
2905: * Process an abstract type declaration
2906: */
2907: sym_t *
1.75 rillig 2908: declare_1_abstract(sym_t *sym)
1.1 cgd 2909: {
1.22 lukem 2910:
1.124 rillig 2911: check_function_definition(sym, true);
1.75 rillig 2912: check_type(sym);
1.76 rillig 2913: return sym;
1.1 cgd 2914: }
2915:
2916: /*
1.138 rillig 2917: * Checks size after declarations of variables and their initialization.
1.1 cgd 2918: */
2919: void
1.75 rillig 2920: check_size(sym_t *dsym)
1.1 cgd 2921: {
1.22 lukem 2922:
1.1 cgd 2923: if (dsym->s_def != DEF)
2924: return;
2925: if (dsym->s_scl == TYPEDEF)
2926: return;
2927: if (dsym->s_type->t_tspec == FUNC)
2928: return;
2929:
2930: if (length(dsym->s_type, dsym->s_name) == 0 &&
2931: dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) {
2932: if (tflag) {
1.94 rillig 2933: /* empty array declaration: %s */
1.1 cgd 2934: warning(190, dsym->s_name);
2935: } else {
1.94 rillig 2936: /* empty array declaration: %s */
1.1 cgd 2937: error(190, dsym->s_name);
2938: }
2939: }
2940: }
2941:
2942: /*
2943: * Mark an object as set if it is not already
2944: */
2945: void
1.75 rillig 2946: mark_as_set(sym_t *sym)
1.1 cgd 2947: {
1.22 lukem 2948:
1.1 cgd 2949: if (!sym->s_set) {
1.122 rillig 2950: sym->s_set = true;
1.84 rillig 2951: UNIQUE_CURR_POS(sym->s_set_pos);
1.1 cgd 2952: }
2953: }
2954:
2955: /*
2956: * Mark an object as used if it is not already
2957: */
2958: void
1.122 rillig 2959: mark_as_used(sym_t *sym, bool fcall, bool szof)
1.1 cgd 2960: {
1.22 lukem 2961:
1.1 cgd 2962: if (!sym->s_used) {
1.122 rillig 2963: sym->s_used = true;
1.84 rillig 2964: UNIQUE_CURR_POS(sym->s_use_pos);
1.1 cgd 2965: }
2966: /*
2967: * for function calls another record is written
2968: *
1.73 rillig 2969: * XXX Should symbols used in sizeof() be treated as used or not?
1.1 cgd 2970: * Probably not, because there is no sense to declare an
2971: * external variable only to get their size.
2972: */
2973: if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN)
2974: outusg(sym);
2975: }
2976:
2977: /*
2978: * Prints warnings for a list of variables and labels (concatenated
2979: * with s_dlnxt) if these are not used or only set.
2980: */
2981: void
1.75 rillig 2982: check_usage(dinfo_t *di)
1.1 cgd 2983: {
2984: sym_t *sym;
1.56 christos 2985: int mklwarn;
1.1 cgd 2986:
1.57 christos 2987: /* for this warning LINTED has no effect */
1.56 christos 2988: mklwarn = lwarn;
2989: lwarn = LWARN_ALL;
1.1 cgd 2990:
1.38 christos 2991: #ifdef DEBUG
1.56 christos 2992: printf("%s, %d: >temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
2993: lwarn);
1.38 christos 2994: #endif
1.10 jpo 2995: for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt)
1.75 rillig 2996: check_usage_sym(di->d_asm, sym);
1.56 christos 2997: lwarn = mklwarn;
1.38 christos 2998: #ifdef DEBUG
1.56 christos 2999: printf("%s, %d: <temp lwarn = %d\n", curr_pos.p_file, curr_pos.p_line,
3000: lwarn);
1.38 christos 3001: #endif
1.1 cgd 3002: }
3003:
3004: /*
3005: * Prints a warning for a single variable or label if it is not used or
3006: * only set.
3007: */
3008: void
1.122 rillig 3009: check_usage_sym(bool novar, sym_t *sym)
1.1 cgd 3010: {
3011: pos_t cpos;
3012:
1.144 rillig 3013: if (sym->s_block_level == -1)
1.1 cgd 3014: return;
3015:
1.85 rillig 3016: cpos = curr_pos;
1.1 cgd 3017:
3018: if (sym->s_kind == FVFT) {
3019: if (sym->s_arg) {
1.75 rillig 3020: check_argument_usage(novar, sym);
1.1 cgd 3021: } else {
1.75 rillig 3022: check_variable_usage(novar, sym);
1.1 cgd 3023: }
1.81 rillig 3024: } else if (sym->s_kind == FLABEL) {
1.75 rillig 3025: check_label_usage(sym);
1.1 cgd 3026: } else if (sym->s_kind == FTAG) {
1.75 rillig 3027: check_tag_usage(sym);
1.1 cgd 3028: }
3029:
1.85 rillig 3030: curr_pos = cpos;
1.1 cgd 3031: }
3032:
3033: static void
1.122 rillig 3034: check_argument_usage(bool novar, sym_t *arg)
1.1 cgd 3035: {
1.22 lukem 3036:
1.92 rillig 3037: lint_assert(arg->s_set);
1.1 cgd 3038:
1.10 jpo 3039: if (novar)
3040: return;
3041:
1.1 cgd 3042: if (!arg->s_used && vflag) {
1.85 rillig 3043: curr_pos = arg->s_def_pos;
1.171 rillig 3044: /* argument '%s' unused in function '%s' */
1.1 cgd 3045: warning(231, arg->s_name, funcsym->s_name);
3046: }
3047: }
3048:
3049: static void
1.122 rillig 3050: check_variable_usage(bool novar, sym_t *sym)
1.1 cgd 3051: {
3052: scl_t sc;
3053: sym_t *xsym;
3054:
1.144 rillig 3055: lint_assert(block_level != 0);
3056: lint_assert(sym->s_block_level != 0);
1.1 cgd 3057:
3058: /* errors in expressions easily cause lots of these warnings */
3059: if (nerr != 0)
3060: return;
3061:
3062: /*
1.73 rillig 3063: * XXX Only variables are checked, although types should
1.1 cgd 3064: * probably also be checked
3065: */
3066: if ((sc = sym->s_scl) != EXTERN && sc != STATIC &&
3067: sc != AUTO && sc != REG) {
3068: return;
3069: }
1.10 jpo 3070:
3071: if (novar)
3072: return;
1.1 cgd 3073:
3074: if (sc == EXTERN) {
3075: if (!sym->s_used && !sym->s_set) {
1.85 rillig 3076: curr_pos = sym->s_def_pos;
1.171 rillig 3077: /* '%s' unused in function '%s' */
1.1 cgd 3078: warning(192, sym->s_name, funcsym->s_name);
3079: }
3080: } else {
3081: if (sym->s_set && !sym->s_used) {
1.85 rillig 3082: curr_pos = sym->s_set_pos;
1.171 rillig 3083: /* '%s' set but not used in function '%s' */
1.1 cgd 3084: warning(191, sym->s_name, funcsym->s_name);
3085: } else if (!sym->s_used) {
1.85 rillig 3086: curr_pos = sym->s_def_pos;
1.171 rillig 3087: /* '%s' unused in function '%s' */
1.1 cgd 3088: warning(192, sym->s_name, funcsym->s_name);
3089: }
3090: }
3091:
3092: if (sc == EXTERN) {
3093: /*
3094: * information about usage is taken over into the symbol
1.73 rillig 3095: * table entry at level 0 if the symbol was locally declared
1.1 cgd 3096: * as an external symbol.
3097: *
3098: * XXX This is wrong for symbols declared static at level 0
3099: * if the usage information stems from sizeof(). This is
3100: * because symbols at level 0 only used in sizeof() are
3101: * considered to not be used.
3102: */
1.83 rillig 3103: if ((xsym = sym->s_ext_sym) != NULL) {
1.1 cgd 3104: if (sym->s_used && !xsym->s_used) {
1.122 rillig 3105: xsym->s_used = true;
1.85 rillig 3106: xsym->s_use_pos = sym->s_use_pos;
1.1 cgd 3107: }
3108: if (sym->s_set && !xsym->s_set) {
1.122 rillig 3109: xsym->s_set = true;
1.85 rillig 3110: xsym->s_set_pos = sym->s_set_pos;
1.1 cgd 3111: }
3112: }
3113: }
3114: }
3115:
3116: static void
1.75 rillig 3117: check_label_usage(sym_t *lab)
1.1 cgd 3118: {
1.22 lukem 3119:
1.144 rillig 3120: lint_assert(block_level == 1);
3121: lint_assert(lab->s_block_level == 1);
1.1 cgd 3122:
3123: if (lab->s_set && !lab->s_used) {
1.85 rillig 3124: curr_pos = lab->s_set_pos;
1.115 rillig 3125: /* label %s unused in function %s */
3126: warning(232, lab->s_name, funcsym->s_name);
1.1 cgd 3127: } else if (!lab->s_set) {
1.85 rillig 3128: curr_pos = lab->s_use_pos;
1.1 cgd 3129: /* undefined label %s */
3130: warning(23, lab->s_name);
3131: }
3132: }
3133:
3134: static void
1.75 rillig 3135: check_tag_usage(sym_t *sym)
1.1 cgd 3136: {
1.22 lukem 3137:
1.132 rillig 3138: if (!is_incomplete(sym->s_type))
1.1 cgd 3139: return;
3140:
1.73 rillig 3141: /* always complain about incomplete tags declared inside blocks */
1.3 jpo 3142: if (!zflag || dcs->d_ctx != EXTERN)
1.1 cgd 3143: return;
3144:
1.85 rillig 3145: curr_pos = sym->s_def_pos;
1.1 cgd 3146: switch (sym->s_type->t_tspec) {
3147: case STRUCT:
3148: /* struct %s never defined */
3149: warning(233, sym->s_name);
3150: break;
3151: case UNION:
3152: /* union %s never defined */
3153: warning(234, sym->s_name);
3154: break;
3155: case ENUM:
3156: /* enum %s never defined */
3157: warning(235, sym->s_name);
3158: break;
3159: default:
1.124 rillig 3160: lint_assert(/*CONSTCOND*/false);
1.1 cgd 3161: }
3162: }
3163:
3164: /*
3165: * Called after the entire translation unit has been parsed.
1.73 rillig 3166: * Changes tentative definitions into definitions.
3167: * Performs some tests on global symbols. Detected problems are:
1.1 cgd 3168: * - defined variables of incomplete type
3169: * - constant variables which are not initialized
3170: * - static symbols which are never used
3171: */
3172: void
1.75 rillig 3173: check_global_symbols(void)
1.1 cgd 3174: {
3175: sym_t *sym;
3176: pos_t cpos;
3177:
1.144 rillig 3178: if (block_level != 0 || dcs->d_next != NULL)
1.1 cgd 3179: norecover();
3180:
1.85 rillig 3181: cpos = curr_pos;
1.1 cgd 3182:
1.3 jpo 3183: for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1.144 rillig 3184: if (sym->s_block_level == -1)
1.1 cgd 3185: continue;
3186: if (sym->s_kind == FVFT) {
1.75 rillig 3187: check_global_variable(sym);
1.1 cgd 3188: } else if (sym->s_kind == FTAG) {
1.75 rillig 3189: check_tag_usage(sym);
1.1 cgd 3190: } else {
1.92 rillig 3191: lint_assert(sym->s_kind == FMEMBER);
1.1 cgd 3192: }
3193: }
3194:
1.85 rillig 3195: curr_pos = cpos;
1.1 cgd 3196: }
3197:
3198: static void
1.120 rillig 3199: check_unused_static_global_variable(const sym_t *sym)
3200: {
3201: curr_pos = sym->s_def_pos;
3202: if (sym->s_type->t_tspec == FUNC) {
3203: if (sym->s_def == DEF) {
3204: if (!sym->s_inline)
3205: /* static function %s unused */
3206: warning(236, sym->s_name);
3207: } else {
3208: /* static function %s declared but not defined */
3209: warning(290, sym->s_name);
3210: }
3211: } else if (!sym->s_set) {
3212: /* static variable %s unused */
3213: warning(226, sym->s_name);
3214: } else {
3215: /* static variable %s set but not used */
3216: warning(307, sym->s_name);
3217: }
3218: }
3219:
3220: static void
3221: check_static_global_variable(const sym_t *sym)
3222: {
3223: if (sym->s_type->t_tspec == FUNC && sym->s_used && sym->s_def != DEF) {
3224: curr_pos = sym->s_use_pos;
3225: /* static function called but not defined: %s() */
3226: error(225, sym->s_name);
3227: }
3228:
3229: if (!sym->s_used)
3230: check_unused_static_global_variable(sym);
3231:
3232: if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) {
3233: curr_pos = sym->s_def_pos;
3234: /* const object %s should have initializer */
3235: warning(227, sym->s_name);
3236: }
3237: }
3238:
3239: static void
3240: check_global_variable(const sym_t *sym)
1.1 cgd 3241: {
1.22 lukem 3242:
1.123 rillig 3243: if (sym->s_scl == TYPEDEF || sym->s_scl == CTCONST)
1.1 cgd 3244: return;
1.22 lukem 3245:
1.119 rillig 3246: if (sym->s_scl == NOSCL)
3247: return; /* May be caused by a syntax error. */
3248:
1.92 rillig 3249: lint_assert(sym->s_scl == EXTERN || sym->s_scl == STATIC);
1.1 cgd 3250:
1.75 rillig 3251: check_global_variable_size(sym);
1.1 cgd 3252:
1.120 rillig 3253: if (sym->s_scl == STATIC)
3254: check_static_global_variable(sym);
1.1 cgd 3255: }
3256:
3257: static void
1.120 rillig 3258: check_global_variable_size(const sym_t *sym)
1.1 cgd 3259: {
1.22 lukem 3260:
1.129 rillig 3261: if (sym->s_def != TDEF)
3262: return;
3263: if (sym->s_type->t_tspec == FUNC)
3264: /*
3265: * this can happen if a syntax error occurred after a
3266: * function declaration
3267: */
3268: return;
3269:
3270: curr_pos = sym->s_def_pos;
3271: if (length(sym->s_type, sym->s_name) == 0 &&
3272: sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) {
3273: if (tflag || (sym->s_scl == EXTERN && !sflag)) {
3274: /* empty array declaration: %s */
3275: warning(190, sym->s_name);
3276: } else {
3277: /* empty array declaration: %s */
3278: error(190, sym->s_name);
1.1 cgd 3279: }
3280: }
3281: }
3282:
3283: /*
3284: * Prints information about location of previous definition/declaration.
3285: */
3286: void
1.121 rillig 3287: print_previous_declaration(int msg, const sym_t *psym)
1.1 cgd 3288: {
3289:
3290: if (!rflag)
3291: return;
3292:
1.6 jpo 3293: if (msg != -1) {
1.173 ! rillig 3294: (message_at)(msg, psym->s_def_pos);
1.6 jpo 3295: } else if (psym->s_def == DEF || psym->s_def == TDEF) {
1.1 cgd 3296: /* previous definition of %s */
1.173 ! rillig 3297: message_at(261, psym->s_def_pos, psym->s_name);
1.1 cgd 3298: } else {
3299: /* previous declaration of %s */
1.173 ! rillig 3300: message_at(260, psym->s_def_pos, psym->s_name);
1.1 cgd 3301: }
3302: }
1.146 rillig 3303:
3304: /*
3305: * Gets a node for a constant and returns the value of this constant
3306: * as integer.
3307: *
3308: * If the node is not constant or too large for int or of type float,
3309: * a warning will be printed.
3310: *
3311: * to_int_constant() should be used only inside declarations. If it is used in
3312: * expressions, it frees the memory used for the expression.
3313: */
3314: int
3315: to_int_constant(tnode_t *tn, bool required)
3316: {
3317: int i;
3318: tspec_t t;
3319: val_t *v;
3320:
3321: v = constant(tn, required);
3322:
3323: if (tn == NULL) {
3324: i = 1;
3325: goto done;
3326: }
3327:
3328: /*
3329: * Abstract declarations are used inside expression. To free
3330: * the memory would be a fatal error.
3331: * We don't free blocks that are inside casts because these
3332: * will be used later to match types.
3333: */
3334: if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT)
1.168 rillig 3335: expr_free_all();
1.146 rillig 3336:
3337: if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) {
3338: i = (int)v->v_ldbl;
3339: /* integral constant expression expected */
3340: error(55);
3341: } else {
3342: i = (int)v->v_quad;
3343: if (is_uinteger(t)) {
3344: if ((uint64_t)v->v_quad > (uint64_t)TARG_INT_MAX) {
3345: /* integral constant too large */
3346: warning(56);
3347: }
3348: } else {
3349: if (v->v_quad > (int64_t)TARG_INT_MAX ||
3350: v->v_quad < (int64_t)TARG_INT_MIN) {
3351: /* integral constant too large */
3352: warning(56);
3353: }
3354: }
3355: }
3356:
3357: done:
3358: free(v);
3359: return i;
3360: }
CVSweb <webmaster@jp.NetBSD.org>