decl.c revision 286614
155714Skris/* $NetBSD: decl.c,v 1.33 2004/06/20 22:20:16 jmc Exp $ */ 255714Skris 355714Skris/* 455714Skris * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 555714Skris * Copyright (c) 1994, 1995 Jochen Pohl 655714Skris * All Rights Reserved. 755714Skris * 855714Skris * Redistribution and use in source and binary forms, with or without 955714Skris * modification, are permitted provided that the following conditions 10109998Smarkm * are met: 1155714Skris * 1. Redistributions of source code must retain the above copyright 12160814Ssimon * notice, this list of conditions and the following disclaimer. 13160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright 14160814Ssimon * notice, this list of conditions and the following disclaimer in the 1555714Skris * documentation and/or other materials provided with the distribution. 1655714Skris * 3. All advertising materials mentioning features or use of this software 1755714Skris * must display the following acknowledgement: 1855714Skris * This product includes software developed by Jochen Pohl for 1955714Skris * The NetBSD Project. 2055714Skris * 4. The name of the author may not be used to endorse or promote products 2155714Skris * derived from this software without specific prior written permission. 2255714Skris * 2355714Skris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2455714Skris * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2555714Skris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2655714Skris * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2755714Skris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2855714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2955714Skris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3055714Skris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3155714Skris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3255714Skris * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3355714Skris */ 3455714Skris 35109998Smarkm#include <sys/cdefs.h> 36109998Smarkm#if defined(__RCSID) && !defined(lint) 37109998Smarkm__RCSID("$NetBSD: decl.c,v 1.33 2004/06/20 22:20:16 jmc Exp $"); 3855714Skris#endif 3955714Skris__FBSDID("$FreeBSD: head/usr.bin/xlint/lint1/decl.c 286614 2015-08-11 02:58:33Z pfg $"); 4055714Skris 4155714Skris#include <sys/param.h> 4255714Skris#include <limits.h> 4355714Skris#include <stdlib.h> 4455714Skris#include <string.h> 4555714Skris 4655714Skris#include "lint1.h" 47194206Ssimon 48194206Ssimonconst char *unnamed = "<unnamed>"; 4955714Skris 5055714Skris/* shared type structures for arithmtic types and void */ 5155714Skrisstatic type_t *typetab; 5255714Skris 5355714Skris/* value of next enumerator during declaration of enum types */ 5455714Skrisint enumval; 5555714Skris 5655714Skris/* 5755714Skris * pointer to top element of a stack which contains informations local 5855714Skris * to nested declarations 5955714Skris */ 6055714Skrisdinfo_t *dcs; 6155714Skris 6255714Skrisstatic type_t *tdeferr(type_t *, tspec_t); 6389837Skrisstatic void settdsym(type_t *, sym_t *); 6455714Skrisstatic tspec_t mrgtspec(tspec_t, tspec_t); 6555714Skrisstatic void align(int, int); 6655714Skrisstatic sym_t *newtag(sym_t *, scl_t, int, int); 6755714Skrisstatic int eqargs(type_t *, type_t *, int *); 6855714Skrisstatic int mnoarg(type_t *, int *); 69160814Ssimonstatic int chkosdef(sym_t *, sym_t *); 70160814Ssimonstatic int chkptdecl(sym_t *, sym_t *); 7155714Skrisstatic sym_t *nsfunc(sym_t *, sym_t *); 7255714Skrisstatic void osfunc(sym_t *, sym_t *); 7355714Skrisstatic void ledecl(sym_t *); 7455714Skrisstatic int chkinit(sym_t *); 7555714Skrisstatic void chkausg(int, sym_t *); 7655714Skrisstatic void chkvusg(int, sym_t *); 7755714Skrisstatic void chklusg(sym_t *); 7855714Skrisstatic void chktusg(sym_t *); 7955714Skrisstatic void chkglvar(sym_t *); 8055714Skrisstatic void glchksz(sym_t *); 8155714Skris 8255714Skris/* 8355714Skris * initializes all global vars used in declarations 8455714Skris */ 8555714Skrisvoid 8655714Skrisinitdecl(void) 8789837Skris{ 8855714Skris int i; 8955714Skris 9055714Skris /* declaration stack */ 91109998Smarkm if ((dcs = calloc(1, sizeof (dinfo_t))) == NULL) 92109998Smarkm nomem(); 93109998Smarkm dcs->d_ctx = EXTERN; 94109998Smarkm dcs->d_ldlsym = &dcs->d_dlsyms; 95109998Smarkm 96109998Smarkm /* type information and classification */ 9768651Skris inittyp(); 98109998Smarkm 99109998Smarkm /* shared type structures */ 100109998Smarkm if ((typetab = calloc(NTSPEC, sizeof (type_t))) == NULL) 101109998Smarkm nomem(); 102109998Smarkm for (i = 0; i < NTSPEC; i++) 10355714Skris typetab[i].t_tspec = NOTSPEC; 104109998Smarkm typetab[CHAR].t_tspec = CHAR; 105109998Smarkm typetab[SCHAR].t_tspec = SCHAR; 106109998Smarkm typetab[UCHAR].t_tspec = UCHAR; 107109998Smarkm typetab[SHORT].t_tspec = SHORT; 10855714Skris typetab[USHORT].t_tspec = USHORT; 109160814Ssimon typetab[INT].t_tspec = INT; 110160814Ssimon typetab[UINT].t_tspec = UINT; 111160814Ssimon typetab[LONG].t_tspec = LONG; 112160814Ssimon typetab[ULONG].t_tspec = ULONG; 113160814Ssimon typetab[QUAD].t_tspec = QUAD; 114160814Ssimon typetab[UQUAD].t_tspec = UQUAD; 115160814Ssimon typetab[FLOAT].t_tspec = FLOAT; 116160814Ssimon typetab[DOUBLE].t_tspec = DOUBLE; 117160814Ssimon typetab[LDOUBLE].t_tspec = LDOUBLE; 118109998Smarkm typetab[VOID].t_tspec = VOID; 119160814Ssimon /* 120160814Ssimon * Next two are not real types. They are only used by the parser 121109998Smarkm * to return keywords "signed" and "unsigned" 122109998Smarkm */ 123160814Ssimon typetab[SIGNED].t_tspec = SIGNED; 12455714Skris typetab[UNSIGN].t_tspec = UNSIGN; 125109998Smarkm} 12655714Skris 127109998Smarkm/* 12855714Skris * Returns a shared type structure vor arithmetic types and void. 129109998Smarkm * 130109998Smarkm * It's important to duplicate this structure (using duptyp() or tdupdyp()) 131109998Smarkm * if it is to be modified (adding qualifiers or anything else). 132109998Smarkm */ 133109998Smarkmtype_t * 13455714Skrisgettyp(tspec_t t) 13555714Skris{ 13655714Skris 13768651Skris return (&typetab[t]); 13868651Skris} 139167612Ssimon 140167612Ssimontype_t * 14155714Skrisduptyp(const type_t *tp) 14268651Skris{ 14368651Skris type_t *ntp; 14468651Skris 145160814Ssimon ntp = getblk(sizeof (type_t)); 146160814Ssimon STRUCT_ASSIGN(*ntp, *tp); 14755714Skris return (ntp); 14855714Skris} 149160814Ssimon 150160814Ssimon/* 151160814Ssimon * Use tduptyp() instead of duptyp() inside expressions (if the 152160814Ssimon * allocated memory should be freed after the expr). 153167612Ssimon */ 154167612Ssimontype_t * 155167612Ssimontduptyp(const type_t *tp) 156160814Ssimon{ 157160814Ssimon type_t *ntp; 158160814Ssimon 159160814Ssimon ntp = tgetblk(sizeof (type_t)); 160160814Ssimon STRUCT_ASSIGN(*ntp, *tp); 161160814Ssimon return (ntp); 162160814Ssimon} 163160814Ssimon 164160814Ssimon/* 165160814Ssimon * Returns 1 if the argument is void or an incomplete array, 166160814Ssimon * struct, union or enum type. 16755714Skris */ 16855714Skrisint 169160814Ssimonincompl(type_t *tp) 17055714Skris{ 17155714Skris tspec_t t; 17255714Skris 17355714Skris if ((t = tp->t_tspec) == VOID) { 17468651Skris return (1); 17568651Skris } else if (t == ARRAY) { 17655714Skris return (tp->t_aincompl); 17755714Skris } else if (t == STRUCT || t == UNION) { 17855714Skris return (tp->t_str->sincompl); 17955714Skris } else if (t == ENUM) { 18055714Skris return (tp->t_enum->eincompl); 181109998Smarkm } 182160814Ssimon return (0); 183296465Sdelphij} 18455714Skris 18555714Skris/* 186109998Smarkm * Set the flag for (in)complete array, struct, union or enum 18755714Skris * types. 18855714Skris */ 18955714Skrisvoid 19055714Skrissetcompl(type_t *tp, int ic) 19155714Skris{ 19255714Skris tspec_t t; 193160814Ssimon 194160814Ssimon if ((t = tp->t_tspec) == ARRAY) { 195160814Ssimon tp->t_aincompl = ic; 196160814Ssimon } else if (t == STRUCT || t == UNION) { 19755714Skris tp->t_str->sincompl = ic; 19855714Skris } else { 19955714Skris if (t != ENUM) 20055714Skris LERROR("setcompl()"); 20155714Skris tp->t_enum->eincompl = ic; 202160814Ssimon } 203160814Ssimon} 204160814Ssimon 205160814Ssimon/* 20655714Skris * Remember the storage class of the current declaration in dcs->d_scl 20755714Skris * (the top element of the declaration stack) and detect multiple 20855714Skris * storage classes. 20955714Skris */ 21055714Skrisvoid 21155714Skrisaddscl(scl_t sc) 212160814Ssimon{ 21355714Skris 214160814Ssimon if (sc == INLINE) { 215160814Ssimon if (dcs->d_inline) 216160814Ssimon /* duplicate '%s' */ 217160814Ssimon warning(10, "inline"); 218160814Ssimon dcs->d_inline = 1; 219160814Ssimon return; 220160814Ssimon } 221160814Ssimon if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC || 222160814Ssimon dcs->d_smod != NOTSPEC || dcs->d_lmod != NOTSPEC) { 223160814Ssimon /* storage class after type is obsolescent */ 224160814Ssimon warning(83); 225160814Ssimon } 226160814Ssimon if (dcs->d_scl == NOSCL) { 227160814Ssimon dcs->d_scl = sc; 228160814Ssimon } else { 229160814Ssimon /* 230160814Ssimon * multiple storage classes. An error will be reported in 231160814Ssimon * deftyp(). 232160814Ssimon */ 233160814Ssimon dcs->d_mscl = 1; 234160814Ssimon } 235160814Ssimon} 236160814Ssimon 23755714Skris/* 23855714Skris * Remember the type, modifier or typedef name returned by the parser 23955714Skris * in *dcs (top element of decl stack). This information is used in 24055714Skris * deftyp() to build the type used for all declarators in this 24155714Skris * declaration. 24255714Skris * 24355714Skris * Is tp->t_typedef 1, the type comes from a previously defined typename. 24455714Skris * Otherwise it comes from a type specifier (int, long, ...) or a 24555714Skris * struct/union/enum tag. 24655714Skris */ 24755714Skrisvoid 24855714Skrisaddtype(type_t *tp) 24955714Skris{ 25055714Skris tspec_t t; 25155714Skris 25255714Skris if (tp->t_typedef) { 25355714Skris if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC || 25455714Skris dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) { 255160814Ssimon /* 25655714Skris * something like "typedef int a; int a b;" 25755714Skris * This should not happen with current grammar. 25855714Skris */ 25955714Skris LERROR("addtype()"); 26055714Skris } 26155714Skris dcs->d_type = tp; 26255714Skris return; 26355714Skris } 26455714Skris 26555714Skris t = tp->t_tspec; 26655714Skris 26755714Skris if (t == STRUCT || t == UNION || t == ENUM) { 26855714Skris /* 26955714Skris * something like "int struct a ..." 27055714Skris * struct/union/enum with anything else is not allowed 27155714Skris */ 27255714Skris if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC || 27355714Skris dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) { 27455714Skris /* 27555714Skris * remember that an error must be reported in 27655714Skris * deftyp(). 27755714Skris */ 27855714Skris dcs->d_terr = 1; 279160814Ssimon dcs->d_atyp = dcs->d_lmod = dcs->d_smod = NOTSPEC; 28055714Skris } 28155714Skris dcs->d_type = tp; 28255714Skris return; 28355714Skris } 28455714Skris 28555714Skris if (dcs->d_type != NULL && !dcs->d_type->t_typedef) { 28655714Skris /* 28755714Skris * something like "struct a int" 28855714Skris * struct/union/enum with anything else is not allowed 28955714Skris */ 29055714Skris dcs->d_terr = 1; 29155714Skris return; 29255714Skris } 29355714Skris 29455714Skris if (t == LONG && dcs->d_lmod == LONG) { 29555714Skris /* "long long" or "long ... long" */ 29655714Skris t = QUAD; 29755714Skris dcs->d_lmod = NOTSPEC; 29855714Skris if (!quadflg) 29955714Skris /* %s C does not support 'long long' */ 30055714Skris (void)c99ism(265, tflag ? "traditional" : "c89"); 30155714Skris } 30255714Skris 30355714Skris if (dcs->d_type != NULL && dcs->d_type->t_typedef) { 30455714Skris /* something like "typedef int a; a long ..." */ 30555714Skris dcs->d_type = tdeferr(dcs->d_type, t); 306109998Smarkm return; 307109998Smarkm } 308109998Smarkm 30955714Skris /* now it can be only a combination of arithmetic types and void */ 31055714Skris if (t == SIGNED || t == UNSIGN) { 311109998Smarkm /* remember specifiers "signed" and "unsigned" in dcs->d_smod */ 312109998Smarkm if (dcs->d_smod != NOTSPEC) 313109998Smarkm /* 314109998Smarkm * more than one "signed" and/or "unsigned"; print 315109998Smarkm * an error in deftyp() 316237998Sjkim */ 317109998Smarkm dcs->d_terr = 1; 318109998Smarkm dcs->d_smod = t; 319109998Smarkm } else if (t == SHORT || t == LONG || t == QUAD) { 320109998Smarkm /* 321109998Smarkm * remember specifiers "short", "long" and "long long" in 322109998Smarkm * dcs->d_lmod 323109998Smarkm */ 324109998Smarkm if (dcs->d_lmod != NOTSPEC) 325109998Smarkm /* more than one, print error in deftyp() */ 326109998Smarkm dcs->d_terr = 1; 327109998Smarkm dcs->d_lmod = t; 328109998Smarkm } else { 329109998Smarkm /* 330109998Smarkm * remember specifiers "void", "char", "int", "float" or 331109998Smarkm * "double" int dcs->d_atyp 332109998Smarkm */ 333109998Smarkm if (dcs->d_atyp != NOTSPEC) 334109998Smarkm /* more than one, print error in deftyp() */ 335109998Smarkm dcs->d_terr = 1; 336109998Smarkm dcs->d_atyp = t; 337109998Smarkm } 338109998Smarkm} 339109998Smarkm 340109998Smarkm/* 341109998Smarkm * called if a list of declaration specifiers contains a typedef name 342109998Smarkm * and other specifiers (except struct, union, enum, typedef name) 343109998Smarkm */ 344109998Smarkmstatic type_t * 345109998Smarkmtdeferr(type_t *td, tspec_t t) 346109998Smarkm{ 347109998Smarkm tspec_t t2; 348109998Smarkm 349109998Smarkm t2 = td->t_tspec; 350109998Smarkm 351109998Smarkm switch (t) { 352109998Smarkm case SIGNED: 353109998Smarkm case UNSIGN: 354109998Smarkm if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG || 355109998Smarkm t2 == QUAD) { 356109998Smarkm if (!tflag) 357109998Smarkm /* modifying typedef with ... */ 358109998Smarkm warning(5, ttab[t].tt_name); 359109998Smarkm td = duptyp(gettyp(mrgtspec(t2, t))); 360109998Smarkm td->t_typedef = 1; 361109998Smarkm return (td); 362109998Smarkm } 363109998Smarkm break; 364109998Smarkm case SHORT: 365109998Smarkm if (t2 == INT || t2 == UINT) { 366109998Smarkm /* modifying typedef with ... */ 367109998Smarkm warning(5, "short"); 368109998Smarkm td = duptyp(gettyp(t2 == INT ? SHORT : USHORT)); 369109998Smarkm td->t_typedef = 1; 370109998Smarkm return (td); 371109998Smarkm } 372109998Smarkm break; 37355714Skris case LONG: 37455714Skris if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG || 37555714Skris t2 == FLOAT || t2 == DOUBLE) { 37655714Skris /* modifying typedef with ... */ 37755714Skris warning(5, "long"); 37855714Skris if (t2 == INT) { 379296465Sdelphij td = gettyp(LONG); 380296465Sdelphij } else if (t2 == UINT) { 38155714Skris td = gettyp(ULONG); 38255714Skris } else if (t2 == LONG) { 383109998Smarkm td = gettyp(QUAD); 384109998Smarkm } else if (t2 == ULONG) { 385109998Smarkm td = gettyp(UQUAD); 386109998Smarkm } else if (t2 == FLOAT) { 38755714Skris td = gettyp(DOUBLE); 388109998Smarkm } else if (t2 == DOUBLE) { 389109998Smarkm td = gettyp(LDOUBLE); 390109998Smarkm } 391109998Smarkm td = duptyp(td); 392109998Smarkm td->t_typedef = 1; 393109998Smarkm return (td); 394296465Sdelphij } 395109998Smarkm break; 396109998Smarkm /* LINTED (enumeration values not handled in switch) */ 397109998Smarkm case NOTSPEC: 398109998Smarkm case USHORT: 39955714Skris case UCHAR: 40055714Skris case SCHAR: 40155714Skris case CHAR: 40255714Skris case FUNC: 40355714Skris case ARRAY: 40455714Skris case PTR: 405296465Sdelphij case ENUM: 40655714Skris case UNION: 407160814Ssimon case STRUCT: 408160814Ssimon case VOID: 409160814Ssimon case LDOUBLE: 410160814Ssimon case DOUBLE: 411160814Ssimon case FLOAT: 412160814Ssimon case UQUAD: 413160814Ssimon case QUAD: 414160814Ssimon case ULONG: 415160814Ssimon case UINT: 416160814Ssimon case INT: 41755714Skris break; 41855714Skris } 419296465Sdelphij 42055714Skris /* Anything other is not accepted. */ 42155714Skris 42255714Skris dcs->d_terr = 1; 42355714Skris return (td); 42455714Skris} 425296465Sdelphij 42655714Skris/* 427160814Ssimon * Remember the symbol of a typedef name (2nd arg) in a struct, union 428160814Ssimon * or enum tag if the typedef name is the first defined for this tag. 429160814Ssimon * 430160814Ssimon * If the tag is unnamed, the typdef name is used for identification 431160814Ssimon * of this tag in lint2. Although its possible that more than one typedef 432160814Ssimon * name is defined for one tag, the first name defined should be unique 433160814Ssimon * if the tag is unnamed. 434160814Ssimon */ 435160814Ssimonstatic void 436160814Ssimonsettdsym(type_t *tp, sym_t *sym) 43755714Skris{ 43855714Skris tspec_t t; 439296465Sdelphij 44055714Skris if ((t = tp->t_tspec) == STRUCT || t == UNION) { 44155714Skris if (tp->t_str->stdef == NULL) 44255714Skris tp->t_str->stdef = sym; 44355714Skris } else if (t == ENUM) { 44455714Skris if (tp->t_enum->etdef == NULL) 44555714Skris tp->t_enum->etdef = sym; 44655714Skris } 44755714Skris} 44855714Skris 44955714Skris/* 45055714Skris * Remember a qualifier which is part of the declaration specifiers 45155714Skris * (and not the declarator) in the top element of the declaration stack. 45259191Skris * Also detect multiple qualifiers of the same kind. 45359191Skris 45459191Skris * The remembered qualifier is used by deftyp() to construct the type 455296465Sdelphij * for all declarators. 45659191Skris */ 457296465Sdelphijvoid 458296465Sdelphijaddqual(tqual_t q) 459296465Sdelphij{ 460296465Sdelphij 461296465Sdelphij if (q == CONST) { 462296465Sdelphij if (dcs->d_const) { 463296465Sdelphij /* duplicate "%s" */ 464296465Sdelphij warning(10, "const"); 465296465Sdelphij } 46659191Skris dcs->d_const = 1; 46759191Skris } else { 46859191Skris if (q != VOLATILE) 46959191Skris LERROR("addqual()"); 47059191Skris if (dcs->d_volatile) { 47155714Skris /* duplicate "%s" */ 47255714Skris warning(10, "volatile"); 47355714Skris } 47455714Skris dcs->d_volatile = 1; 47555714Skris } 47655714Skris} 47755714Skris 47855714Skris/* 479160814Ssimon * Go to the next declaration level (structs, nested structs, blocks, 480160814Ssimon * argument declaration lists ...) 48155714Skris */ 482160814Ssimonvoid 483160814Ssimonpushdecl(scl_t sc) 484160814Ssimon{ 485160814Ssimon dinfo_t *di; 486160814Ssimon 487160814Ssimon if (dflag) 488160814Ssimon (void)printf("pushdecl(%d)\n", (int)sc); 489160814Ssimon 490160814Ssimon /* put a new element on the declaration stack */ 491160814Ssimon if ((di = calloc(1, sizeof (dinfo_t))) == NULL) 492160814Ssimon nomem(); 493160814Ssimon di->d_nxt = dcs; 49455714Skris dcs = di; 49555714Skris di->d_ctx = sc; 49655714Skris di->d_ldlsym = &di->d_dlsyms; 49755714Skris} 49855714Skris 499237998Sjkim/* 50055714Skris * Go back to previous declaration level 50155714Skris */ 50255714Skrisvoid 50355714Skrispopdecl(void) 50455714Skris{ 50555714Skris dinfo_t *di; 506296465Sdelphij 50755714Skris if (dflag) 50855714Skris (void)printf("popdecl(%d)\n", (int)dcs->d_ctx); 50955714Skris 51055714Skris if (dcs->d_nxt == NULL) 51155714Skris LERROR("popdecl()"); 51255714Skris di = dcs; 51355714Skris dcs = di->d_nxt; 51455714Skris switch (di->d_ctx) { 51555714Skris case EXTERN: 51655714Skris /* there is nothing after external declarations */ 51755714Skris LERROR("popdecl()"); 51855714Skris /* NOTREACHED */ 51955714Skris case MOS: 52055714Skris case MOU: 52155714Skris case ENUMCON: 52255714Skris /* 52355714Skris * Symbols declared in (nested) structs or enums are 52455714Skris * part of the next level (they are removed from the 52555714Skris * symbol table if the symbols of the outher level are 52655714Skris * removed) 52755714Skris */ 52855714Skris if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL) 52955714Skris dcs->d_ldlsym = di->d_ldlsym; 53055714Skris break; 53155714Skris case ARG: 53255714Skris /* 53355714Skris * All symbols in dcs->d_dlsyms are introduced in old style 53455714Skris * argument declarations (it's not clean, but possible). 53555714Skris * They are appended to the list of symbols declared in 53655714Skris * an old style argument identifier list or a new style 53755714Skris * parameter type list. 53855714Skris */ 53955714Skris if (di->d_dlsyms != NULL) { 54055714Skris *di->d_ldlsym = dcs->d_fpsyms; 54155714Skris dcs->d_fpsyms = di->d_dlsyms; 54255714Skris } 54355714Skris break; 54455714Skris case ABSTRACT: 54555714Skris /* 54655714Skris * casts and sizeof 54755714Skris * Append all symbols declared in the abstract declaration 54855714Skris * to the list of symbols declared in the surrounding decl. 54955714Skris * or block. 55055714Skris * XXX I'm not sure whether they should be removed from the 55155714Skris * symbol table now or later. 552296465Sdelphij */ 553296465Sdelphij if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL) 55459191Skris dcs->d_ldlsym = di->d_ldlsym; 55559191Skris break; 55655714Skris case AUTO: 55755714Skris /* check usage of local vars */ 55855714Skris chkusage(di); 55955714Skris /* FALLTHROUGH */ 56055714Skris case PARG: 56155714Skris /* usage of arguments will be checked by funcend() */ 56255714Skris rmsyms(di->d_dlsyms); 563109998Smarkm break; 564160814Ssimon default: 565296465Sdelphij LERROR("popdecl()"); 566296465Sdelphij } 567160814Ssimon free(di); 568296465Sdelphij} 56955714Skris 57055714Skris/* 57155714Skris * Set flag d_asm in all declaration stack elements up to the 57255714Skris * outermost one. 57355714Skris * 57455714Skris * This is used to mark compound statements which have, possibly in 57555714Skris * nested compound statements, asm statements. For these compound 57655714Skris * statements no warnings about unused or unitialized variables are 57755714Skris * printed. 578160814Ssimon * 579296465Sdelphij * There is no need to clear d_asm in dinfo structs with context AUTO, 580296465Sdelphij * because these structs are freed at the end of the compound statement. 581296465Sdelphij * But it must be cleard in the outermost dinfo struct, which has 582296465Sdelphij * context EXTERN. This could be done in clrtyp() and would work for 583296465Sdelphij * C, but not for C++ (due to mixed statements and declarations). Thus 58455714Skris * we clear it in glclup(), which is used to do some cleanup after 58555714Skris * global declarations/definitions. 586296465Sdelphij */ 587296465Sdelphijvoid 58855714Skrissetasm(void) 589296465Sdelphij{ 59055714Skris dinfo_t *di; 59155714Skris 59255714Skris for (di = dcs; di != NULL; di = di->d_nxt) 59355714Skris di->d_asm = 1; 594160814Ssimon} 59559191Skris 59659191Skris/* 59759191Skris * Clean all elements of the top element of declaration stack which 59859191Skris * will be used by the next declaration 59959191Skris */ 60059191Skrisvoid 60159191Skrisclrtyp(void) 602296465Sdelphij{ 603296465Sdelphij 604296465Sdelphij dcs->d_atyp = dcs->d_smod = dcs->d_lmod = NOTSPEC; 605296465Sdelphij dcs->d_scl = NOSCL; 606296465Sdelphij dcs->d_type = NULL; 60755714Skris dcs->d_const = dcs->d_volatile = 0; 60855714Skris dcs->d_inline = 0; 60955714Skris dcs->d_mscl = dcs->d_terr = 0; 610296465Sdelphij dcs->d_nedecl = 0; 611296465Sdelphij dcs->d_notyp = 0; 61255714Skris} 61355714Skris 61455714Skris/* 615109998Smarkm * Create a type structure from the informations gathered in 616296465Sdelphij * the declaration stack. 617167612Ssimon * Complain about storage classes which are not possible in current 61855714Skris * context. 619296465Sdelphij */ 620296465Sdelphijvoid 621296465Sdelphijdeftyp(void) 622296465Sdelphij{ 62355714Skris tspec_t t, s, l; 624296465Sdelphij type_t *tp; 62555714Skris scl_t scl; 62655714Skris 62755714Skris t = dcs->d_atyp; /* CHAR, INT, FLOAT, DOUBLE, VOID */ 628296465Sdelphij s = dcs->d_smod; /* SIGNED, UNSIGNED */ 629296465Sdelphij l = dcs->d_lmod; /* SHORT, LONG, QUAD */ 63055714Skris tp = dcs->d_type; 63155714Skris scl = dcs->d_scl; 63255714Skris 63355714Skris if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && tp == NULL) 634296465Sdelphij dcs->d_notyp = 1; 635296465Sdelphij 636296465Sdelphij if (tp != NULL && (t != NOTSPEC || s != NOTSPEC || l != NOTSPEC)) { 637296465Sdelphij /* should never happen */ 63855714Skris LERROR("deftyp()"); 63955714Skris } 640296465Sdelphij 641296465Sdelphij if (tp == NULL) { 64255714Skris switch (t) { 643109998Smarkm case NOTSPEC: 644296465Sdelphij t = INT; 645296465Sdelphij /* FALLTHROUGH */ 646296465Sdelphij case INT: 64755714Skris if (s == NOTSPEC) 648296465Sdelphij s = SIGNED; 649296465Sdelphij break; 650109998Smarkm case CHAR: 651296465Sdelphij if (l != NOTSPEC) { 652296465Sdelphij dcs->d_terr = 1; 65355714Skris l = NOTSPEC; 65455714Skris } 65555714Skris break; 656296465Sdelphij case FLOAT: 657296465Sdelphij if (l == LONG) { 65859191Skris l = NOTSPEC; 659296465Sdelphij t = DOUBLE; 660296465Sdelphij if (!tflag) 66155714Skris /* use 'double' instead of ... */ 662109998Smarkm warning(6); 663296465Sdelphij } 664296465Sdelphij break; 665109998Smarkm case DOUBLE: 666296465Sdelphij if (l == LONG) { 667296465Sdelphij l = NOTSPEC; 668109998Smarkm t = LDOUBLE; 669109998Smarkm if (tflag) 670109998Smarkm /* 'long double' is illegal in ... */ 671296465Sdelphij warning(266); 672109998Smarkm } 673296465Sdelphij break; 674296465Sdelphij case VOID: 675296465Sdelphij break; 676109998Smarkm default: 677109998Smarkm LERROR("deftyp()"); 678296465Sdelphij } 679296465Sdelphij if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) { 680296465Sdelphij dcs->d_terr = 1; 681296465Sdelphij l = s = NOTSPEC; 682296465Sdelphij } 68355714Skris if (l != NOTSPEC) 68455714Skris t = l; 68555714Skris dcs->d_type = gettyp(mrgtspec(t, s)); 68655714Skris } 68755714Skris 68859191Skris if (dcs->d_mscl) { 68955714Skris /* only one storage class allowed */ 69055714Skris error(7); 691237998Sjkim } 69255714Skris if (dcs->d_terr) { 69355714Skris /* illegal type combination */ 69455714Skris error(4); 69555714Skris } 69655714Skris 69755714Skris if (dcs->d_ctx == EXTERN) { 69855714Skris if (scl == REG || scl == AUTO) { 69955714Skris /* illegal storage class */ 70055714Skris error(8); 70155714Skris scl = NOSCL; 70255714Skris } 70355714Skris } else if (dcs->d_ctx == ARG || dcs->d_ctx == PARG) { 70455714Skris if (scl != NOSCL && scl != REG) { 70555714Skris /* only "register" valid ... */ 70655714Skris error(9); 70755714Skris scl = NOSCL; 70855714Skris } 709279265Sdelphij } 71055714Skris 71155714Skris dcs->d_scl = scl; 71255714Skris 71355714Skris if (dcs->d_const && dcs->d_type->t_const) { 71455714Skris if (!dcs->d_type->t_typedef) 71555714Skris LERROR("deftyp()"); 71655714Skris /* typedef already qualified with "%s" */ 717279265Sdelphij warning(68, "const"); 71855714Skris } 71955714Skris if (dcs->d_volatile && dcs->d_type->t_volatile) { 72055714Skris if (!dcs->d_type->t_typedef) 72155714Skris LERROR("deftyp()"); 72255714Skris /* typedef already qualified with "%s" */ 72355714Skris warning(68, "volatile"); 724 } 725 726 if (dcs->d_const || dcs->d_volatile) { 727 dcs->d_type = duptyp(dcs->d_type); 728 dcs->d_type->t_const |= dcs->d_const; 729 dcs->d_type->t_volatile |= dcs->d_volatile; 730 } 731} 732 733/* 734 * Merge type specifiers (char, ..., long long, signed, unsigned). 735 */ 736static tspec_t 737mrgtspec(tspec_t t, tspec_t s) 738{ 739 740 if (s == SIGNED || s == UNSIGN) { 741 if (t == CHAR) { 742 t = s == SIGNED ? SCHAR : UCHAR; 743 } else if (t == SHORT) { 744 t = s == SIGNED ? SHORT : USHORT; 745 } else if (t == INT) { 746 t = s == SIGNED ? INT : UINT; 747 } else if (t == LONG) { 748 t = s == SIGNED ? LONG : ULONG; 749 } else if (t == QUAD) { 750 t = s == SIGNED ? QUAD : UQUAD; 751 } 752 } 753 754 return (t); 755} 756 757/* 758 * Return the length of a type in bit. 759 * 760 * Printing a message if the outhermost dimension of an array is 0 must 761 * be done by the caller. All other problems are reported by length() 762 * if name is not NULL. 763 */ 764int 765length(type_t *tp, const char *name) 766{ 767 int elem, elsz; 768 769 elem = 1; 770 while (tp && tp->t_tspec == ARRAY) { 771 elem *= tp->t_dim; 772 tp = tp->t_subt; 773 } 774 if (tp == NULL) 775 return -1; 776 777 switch (tp->t_tspec) { 778 case FUNC: 779 /* compiler takes size of function */ 780 LERROR("%s", msgs[12]); 781 /* NOTREACHED */ 782 case STRUCT: 783 case UNION: 784 if (incompl(tp) && name != NULL) { 785 /* incomplete structure or union %s: %s */ 786 error(31, tp->t_str->stag->s_name, name); 787 } 788 elsz = tp->t_str->size; 789 break; 790 case ENUM: 791 if (incompl(tp) && name != NULL) { 792 /* incomplete enum type: %s */ 793 warning(13, name); 794 } 795 /* FALLTHROUGH */ 796 default: 797 elsz = size(tp->t_tspec); 798 if (elsz <= 0) 799 LERROR("length()"); 800 break; 801 } 802 return (elem * elsz); 803} 804 805/* 806 * Get the alignment of the given type in bits. 807 */ 808int 809getbound(type_t *tp) 810{ 811 int a; 812 tspec_t t; 813 814 while (tp && tp->t_tspec == ARRAY) 815 tp = tp->t_subt; 816 817 if (tp == NULL) 818 return -1; 819 820 if ((t = tp->t_tspec) == STRUCT || t == UNION) { 821 a = tp->t_str->align; 822 } else if (t == FUNC) { 823 /* compiler takes alignment of function */ 824 error(14); 825 a = LINT_ALIGN(1) * CHAR_BIT; 826 } else { 827 if ((a = size(t)) == 0) { 828 a = CHAR_BIT; 829 } else if (a > LINT_ALIGN(1) * CHAR_BIT) { 830 a = LINT_ALIGN(1) * CHAR_BIT; 831 } 832 } 833 if (a < CHAR_BIT || a > LINT_ALIGN(1) * CHAR_BIT) 834 LERROR("getbound()"); 835 return (a); 836} 837 838/* 839 * Concatenate two lists of symbols by s_nxt. Used by declarations of 840 * struct/union/enum elements and parameters. 841 */ 842sym_t * 843lnklst(sym_t *l1, sym_t *l2) 844{ 845 sym_t *l; 846 847 if ((l = l1) == NULL) 848 return (l2); 849 while (l1->s_nxt != NULL) 850 l1 = l1->s_nxt; 851 l1->s_nxt = l2; 852 return (l); 853} 854 855/* 856 * Check if the type of the given symbol is valid and print an error 857 * message if it is not. 858 * 859 * Invalid types are: 860 * - arrays of incomlete types or functions 861 * - functions returning arrays or functions 862 * - void types other than type of function or pointer 863 */ 864void 865chktyp(sym_t *sym) 866{ 867 tspec_t to, t; 868 type_t **tpp, *tp; 869 870 tpp = &sym->s_type; 871 to = NOTSPEC; 872 while ((tp = *tpp) != NULL) { 873 t = tp->t_tspec; 874 /* 875 * If this is the type of an old style function definition, 876 * a better warning is printed in funcdef(). 877 */ 878 if (t == FUNC && !tp->t_proto && 879 !(to == NOTSPEC && sym->s_osdef)) { 880 if (sflag && hflag) 881 /* function declaration is not a prototype */ 882 warning(287); 883 } 884 if (to == FUNC) { 885 if (t == FUNC || t == ARRAY) { 886 /* function returns illegal type */ 887 error(15); 888 if (t == FUNC) { 889 *tpp = incref(*tpp, PTR); 890 } else { 891 *tpp = incref((*tpp)->t_subt, PTR); 892 } 893 return; 894 } else if (tp->t_const || tp->t_volatile) { 895 if (sflag) { /* XXX oder better !tflag ? */ 896 /* function cannot return const... */ 897 warning(228); 898 } 899 } 900 } if (to == ARRAY) { 901 if (t == FUNC) { 902 /* array of function is illegal */ 903 error(16); 904 *tpp = gettyp(INT); 905 return; 906 } else if (t == ARRAY && tp->t_dim == 0) { 907 /* null dimension */ 908 error(17); 909 return; 910 } else if (t == VOID) { 911 /* illegal use of void */ 912 error(18); 913 *tpp = gettyp(INT); 914#if 0 /* errors are produced by length() */ 915 } else if (incompl(tp)) { 916 /* array of incomplete type */ 917 if (sflag) { 918 error(301); 919 } else { 920 warning(301); 921 } 922#endif 923 } 924 } else if (to == NOTSPEC && t == VOID) { 925 if (dcs->d_ctx == PARG) { 926 if (sym->s_scl != ABSTRACT) { 927 if (sym->s_name == unnamed) 928 LERROR("chktyp()"); 929 /* void param cannot have name: %s */ 930 error(61, sym->s_name); 931 *tpp = gettyp(INT); 932 } 933 } else if (dcs->d_ctx == ABSTRACT) { 934 /* ok */ 935 } else if (sym->s_scl != TYPEDEF) { 936 /* void type for %s */ 937 error(19, sym->s_name); 938 *tpp = gettyp(INT); 939 } 940 } 941 if (t == VOID && to != PTR) { 942 if (tp->t_const || tp->t_volatile) { 943 /* inappropriate qualifiers with "void" */ 944 warning(69); 945 tp->t_const = tp->t_volatile = 0; 946 } 947 } 948 tpp = &tp->t_subt; 949 to = t; 950 } 951} 952 953/* 954 * Process the declarator of a struct/union element. 955 */ 956sym_t * 957decl1str(sym_t *dsym) 958{ 959 type_t *tp; 960 tspec_t t; 961 int sz, len; 962 int o = 0; /* Appease gcc */ 963 scl_t sc; 964 965 if ((sc = dsym->s_scl) != MOS && sc != MOU) 966 LERROR("decl1str()"); 967 968 if (dcs->d_rdcsym != NULL) { 969 if ((sc = dcs->d_rdcsym->s_scl) != MOS && sc != MOU) 970 /* should be ensured by storesym() */ 971 LERROR("decl1str()"); 972 if (dsym->s_styp == dcs->d_rdcsym->s_styp) { 973 /* duplicate member name: %s */ 974 error(33, dsym->s_name); 975 rmsym(dcs->d_rdcsym); 976 } 977 } 978 979 chktyp(dsym); 980 981 t = (tp = dsym->s_type)->t_tspec; 982 983 if (dsym->s_field) { 984 /* 985 * bit field 986 * 987 * only unsigned and signed int are portable bit-field types 988 * (at least in ANSI C, in traditional C only unsigned int) 989 */ 990 if (t == CHAR || t == UCHAR || t == SCHAR || 991 t == SHORT || t == USHORT || t == ENUM) { 992 if (bitfieldtype_ok == 0) { 993 if (sflag) { 994 char buf[64]; 995 /* 996 * bit-field type '%s' invalid in 997 * ANSI C 998 */ 999 warning(273, 1000 tyname(buf, sizeof(buf), tp)); 1001 } else if (pflag) { 1002 /* nonportable bit-field type */ 1003 warning(34); 1004 } 1005 } 1006 } else if (t == INT && dcs->d_smod == NOTSPEC) { 1007 if (pflag && bitfieldtype_ok == 0) { 1008 /* nonportable bit-field type */ 1009 warning(34); 1010 } 1011 } else if (t != INT && t != UINT) { 1012 /* 1013 * Non-integer types are always illegal for 1014 * bitfields, regardless of BITFIELDTYPE. 1015 * Integer types not dealt with above are 1016 * okay only if BITFIELDTYPE is in effect. 1017 */ 1018 if (bitfieldtype_ok == 0 || isityp(t) == 0) { 1019 /* illegal bit-field type */ 1020 error(35); 1021 sz = tp->t_flen; 1022 dsym->s_type = tp = duptyp(gettyp(t = INT)); 1023 if ((tp->t_flen = sz) > size(t)) 1024 tp->t_flen = size(t); 1025 } 1026 } 1027 if ((len = tp->t_flen) < 0 || len > size(t)) { 1028 /* illegal bit-field size */ 1029 error(36); 1030 tp->t_flen = size(t); 1031 } else if (len == 0 && dsym->s_name != unnamed) { 1032 /* zero size bit-field */ 1033 error(37); 1034 tp->t_flen = size(t); 1035 } 1036 if (dsym->s_scl == MOU) { 1037 /* illegal use of bit-field */ 1038 error(41); 1039 dsym->s_type->t_isfield = 0; 1040 dsym->s_field = 0; 1041 } 1042 } else if (t == FUNC) { 1043 /* function illegal in structure or union */ 1044 error(38); 1045 dsym->s_type = tp = incref(tp, t = PTR); 1046 } 1047 1048 /* 1049 * bit-fields of length 0 are not warned about because length() 1050 * does not return the length of the bit-field but the length 1051 * of the type the bit-field is packed in (its ok) 1052 */ 1053 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) { 1054 if (t == ARRAY && dsym->s_type->t_dim == 0) { 1055 /* illegal zero sized structure member: %s */ 1056 c99ism(39, dsym->s_name); 1057 } 1058 } 1059 1060 if (dcs->d_ctx == MOU) { 1061 o = dcs->d_offset; 1062 dcs->d_offset = 0; 1063 } 1064 if (dsym->s_field) { 1065 align(getbound(tp), tp->t_flen); 1066 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t); 1067 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad; 1068 dcs->d_offset += tp->t_flen; 1069 } else { 1070 align(getbound(tp), 0); 1071 dsym->s_value.v_quad = dcs->d_offset; 1072 dcs->d_offset += sz; 1073 } 1074 if (dcs->d_ctx == MOU) { 1075 if (o > dcs->d_offset) 1076 dcs->d_offset = o; 1077 } 1078 1079 chkfdef(dsym, 0); 1080 1081 /* 1082 * Clear the BITFIELDTYPE indicator after processing each 1083 * structure element. 1084 */ 1085 bitfieldtype_ok = 0; 1086 1087 return (dsym); 1088} 1089 1090/* 1091 * Aligns next structure element as required. 1092 * 1093 * al contains the required alignment, len the length of a bit-field. 1094 */ 1095static void 1096align(int al, int len) 1097{ 1098 int no; 1099 1100 /* 1101 * The alignment of the current element becomes the alignment of 1102 * the struct/union if it is larger than the current alignment 1103 * of the struct/union. 1104 */ 1105 if (al > dcs->d_stralign) 1106 dcs->d_stralign = al; 1107 1108 no = (dcs->d_offset + (al - 1)) & ~(al - 1); 1109 if (len == 0 || dcs->d_offset + len > no) 1110 dcs->d_offset = no; 1111} 1112 1113/* 1114 * Remember the width of the field in its type structure. 1115 */ 1116sym_t * 1117bitfield(sym_t *dsym, int len) 1118{ 1119 1120 if (dsym == NULL) { 1121 dsym = getblk(sizeof (sym_t)); 1122 dsym->s_name = unnamed; 1123 dsym->s_kind = FMOS; 1124 dsym->s_scl = MOS; 1125 dsym->s_type = gettyp(UINT); 1126 dsym->s_blklev = -1; 1127 } 1128 dsym->s_type = duptyp(dsym->s_type); 1129 dsym->s_type->t_isfield = 1; 1130 dsym->s_type->t_flen = len; 1131 dsym->s_field = 1; 1132 return (dsym); 1133} 1134 1135/* 1136 * Collect informations about a sequence of asterisks and qualifiers 1137 * in a list of type pqinf_t. 1138 * Qualifiers refer always to the left asterisk. The rightmost asterisk 1139 * will be at the top of the list. 1140 */ 1141pqinf_t * 1142mergepq(pqinf_t *p1, pqinf_t *p2) 1143{ 1144 pqinf_t *p; 1145 1146 if (p2->p_pcnt != 0) { 1147 /* left '*' at the end of the list */ 1148 for (p = p2; p->p_nxt != NULL; p = p->p_nxt) 1149 continue; 1150 p->p_nxt = p1; 1151 return (p2); 1152 } else { 1153 if (p2->p_const) { 1154 if (p1->p_const) { 1155 /* duplicate %s */ 1156 warning(10, "const"); 1157 } 1158 p1->p_const = 1; 1159 } 1160 if (p2->p_volatile) { 1161 if (p1->p_volatile) { 1162 /* duplicate %s */ 1163 warning(10, "volatile"); 1164 } 1165 p1->p_volatile = 1; 1166 } 1167 free(p2); 1168 return (p1); 1169 } 1170} 1171 1172/* 1173 * Followint 3 functions extend the type of a declarator with 1174 * pointer, function and array types. 1175 * 1176 * The current type is the type built by deftyp() (dcs->d_type) and 1177 * pointer, function and array types already added for this 1178 * declarator. The new type extension is inserted between both. 1179 */ 1180sym_t * 1181addptr(sym_t *decl, pqinf_t *pi) 1182{ 1183 type_t **tpp, *tp; 1184 pqinf_t *npi; 1185 1186 tpp = &decl->s_type; 1187 while (*tpp && *tpp != dcs->d_type) 1188 tpp = &(*tpp)->t_subt; 1189 if (*tpp == NULL) 1190 return decl; 1191 1192 while (pi != NULL) { 1193 *tpp = tp = getblk(sizeof (type_t)); 1194 tp->t_tspec = PTR; 1195 tp->t_const = pi->p_const; 1196 tp->t_volatile = pi->p_volatile; 1197 *(tpp = &tp->t_subt) = dcs->d_type; 1198 npi = pi->p_nxt; 1199 free(pi); 1200 pi = npi; 1201 } 1202 return (decl); 1203} 1204 1205/* 1206 * If a dimension was specified, dim is 1, otherwise 0 1207 * n is the specified dimension 1208 */ 1209sym_t * 1210addarray(sym_t *decl, int dim, int n) 1211{ 1212 type_t **tpp, *tp; 1213 1214 tpp = &decl->s_type; 1215 while (*tpp && *tpp != dcs->d_type) 1216 tpp = &(*tpp)->t_subt; 1217 if (*tpp == NULL) 1218 return decl; 1219 1220 *tpp = tp = getblk(sizeof (type_t)); 1221 tp->t_tspec = ARRAY; 1222 tp->t_subt = dcs->d_type; 1223 tp->t_dim = n; 1224 1225 if (n < 0) { 1226 /* negative array dimension */ 1227 error(20, n); 1228 n = 0; 1229 } else if (n == 0 && dim) { 1230 /* zero array dimension */ 1231 c99ism(322, dim); 1232 } else if (n == 0 && !dim) { 1233 /* is incomplete type */ 1234 setcompl(tp, 1); 1235 } 1236 1237 return (decl); 1238} 1239 1240sym_t * 1241addfunc(sym_t *decl, sym_t *args) 1242{ 1243 type_t **tpp, *tp; 1244 1245 if (dcs->d_proto) { 1246 if (tflag) 1247 /* function prototypes are illegal in traditional C */ 1248 warning(270); 1249 args = nsfunc(decl, args); 1250 } else { 1251 osfunc(decl, args); 1252 } 1253 1254 /* 1255 * The symbols are removed from the symbol table by popdecl() after 1256 * addfunc(). To be able to restore them if this is a function 1257 * definition, a pointer to the list of all symbols is stored in 1258 * dcs->d_nxt->d_fpsyms. Also a list of the arguments (concatenated 1259 * by s_nxt) is stored in dcs->d_nxt->d_fargs. 1260 * (dcs->d_nxt must be used because *dcs is the declaration stack 1261 * element created for the list of params and is removed after 1262 * addfunc()) 1263 */ 1264 if (dcs->d_nxt->d_ctx == EXTERN && 1265 decl->s_type == dcs->d_nxt->d_type) { 1266 dcs->d_nxt->d_fpsyms = dcs->d_dlsyms; 1267 dcs->d_nxt->d_fargs = args; 1268 } 1269 1270 tpp = &decl->s_type; 1271 while (*tpp && *tpp != dcs->d_nxt->d_type) 1272 tpp = &(*tpp)->t_subt; 1273 if (*tpp == NULL) 1274 return decl; 1275 1276 *tpp = tp = getblk(sizeof (type_t)); 1277 tp->t_tspec = FUNC; 1278 tp->t_subt = dcs->d_nxt->d_type; 1279 if ((tp->t_proto = dcs->d_proto) != 0) 1280 tp->t_args = args; 1281 tp->t_vararg = dcs->d_vararg; 1282 1283 return (decl); 1284} 1285 1286/* 1287 * Called for new style function declarations. 1288 */ 1289/* ARGSUSED */ 1290static sym_t * 1291nsfunc(sym_t *decl, sym_t *args) 1292{ 1293 sym_t *arg, *sym; 1294 scl_t sc; 1295 int n; 1296 1297 /* 1298 * Declarations of structs/unions/enums in param lists are legal, 1299 * but senseless. 1300 */ 1301 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) { 1302 sc = sym->s_scl; 1303 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) { 1304 /* dubious tag declaration: %s %s */ 1305 warning(85, scltoa(sc), sym->s_name); 1306 } 1307 } 1308 1309 n = 1; 1310 for (arg = args; arg != NULL; arg = arg->s_nxt) { 1311 if (arg->s_type->t_tspec == VOID) { 1312 if (n > 1 || arg->s_nxt != NULL) { 1313 /* "void" must be sole parameter */ 1314 error(60); 1315 arg->s_type = gettyp(INT); 1316 } 1317 } 1318 n++; 1319 } 1320 1321 /* return NULL if first param is VOID */ 1322 return (args != NULL && args->s_type->t_tspec != VOID ? args : NULL); 1323} 1324 1325/* 1326 * Called for old style function declarations. 1327 */ 1328static void 1329osfunc(sym_t *decl, sym_t *args) 1330{ 1331 1332 /* 1333 * Remember list of params only if this is really seams to be 1334 * a function definition. 1335 */ 1336 if (dcs->d_nxt->d_ctx == EXTERN && 1337 decl->s_type == dcs->d_nxt->d_type) { 1338 /* 1339 * We assume that this becomes a function definition. If 1340 * we are wrong, its corrected in chkfdef(). 1341 */ 1342 if (args != NULL) { 1343 decl->s_osdef = 1; 1344 decl->s_args = args; 1345 } 1346 } else { 1347 if (args != NULL) 1348 /* function prototype parameters must have types */ 1349 warning(62); 1350 } 1351} 1352 1353/* 1354 * Lists of Identifiers in functions declarations are allowed only if 1355 * its also a function definition. If this is not the case, print a 1356 * error message. 1357 */ 1358void 1359chkfdef(sym_t *sym, int msg) 1360{ 1361 1362 if (sym->s_osdef) { 1363 if (msg) { 1364 /* incomplete or misplaced function definition */ 1365 error(22); 1366 } 1367 sym->s_osdef = 0; 1368 sym->s_args = NULL; 1369 } 1370} 1371 1372/* 1373 * Process the name in a declarator. 1374 * If the symbol does already exists, a new one is created. 1375 * The symbol becomes one of the storage classes EXTERN, STATIC, AUTO or 1376 * TYPEDEF. 1377 * s_def and s_reg are valid after dname(). 1378 */ 1379sym_t * 1380dname(sym_t *sym) 1381{ 1382 scl_t sc = NOSCL; 1383 1384 if (sym->s_scl == NOSCL) { 1385 dcs->d_rdcsym = NULL; 1386 } else if (sym->s_defarg) { 1387 sym->s_defarg = 0; 1388 dcs->d_rdcsym = NULL; 1389 } else { 1390 dcs->d_rdcsym = sym; 1391 sym = pushdown(sym); 1392 } 1393 1394 switch (dcs->d_ctx) { 1395 case MOS: 1396 case MOU: 1397 /* Parent setzen */ 1398 sym->s_styp = dcs->d_tagtyp->t_str; 1399 sym->s_def = DEF; 1400 sym->s_value.v_tspec = INT; 1401 sc = dcs->d_ctx; 1402 break; 1403 case EXTERN: 1404 /* 1405 * static and external symbols without "extern" are 1406 * considered to be tentative defined, external 1407 * symbols with "extern" are declared, and typedef names 1408 * are defined. Tentative defined and declared symbols 1409 * may become defined if an initializer is present or 1410 * this is a function definition. 1411 */ 1412 if ((sc = dcs->d_scl) == NOSCL) { 1413 sc = EXTERN; 1414 sym->s_def = TDEF; 1415 } else if (sc == STATIC) { 1416 sym->s_def = TDEF; 1417 } else if (sc == TYPEDEF) { 1418 sym->s_def = DEF; 1419 } else if (sc == EXTERN) { 1420 sym->s_def = DECL; 1421 } else { 1422 LERROR("dname()"); 1423 } 1424 break; 1425 case PARG: 1426 sym->s_arg = 1; 1427 /* FALLTHROUGH */ 1428 case ARG: 1429 if ((sc = dcs->d_scl) == NOSCL) { 1430 sc = AUTO; 1431 } else if (sc == REG) { 1432 sym->s_reg = 1; 1433 sc = AUTO; 1434 } else { 1435 LERROR("dname()"); 1436 } 1437 sym->s_def = DEF; 1438 break; 1439 case AUTO: 1440 if ((sc = dcs->d_scl) == NOSCL) { 1441 /* 1442 * XXX somewhat ugly because we dont know whether 1443 * this is AUTO or EXTERN (functions). If we are 1444 * wrong it must be corrected in decl1loc(), where 1445 * we have the necessary type information. 1446 */ 1447 sc = AUTO; 1448 sym->s_def = DEF; 1449 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) { 1450 sym->s_def = DEF; 1451 } else if (sc == REG) { 1452 sym->s_reg = 1; 1453 sc = AUTO; 1454 sym->s_def = DEF; 1455 } else if (sc == EXTERN) { 1456 sym->s_def = DECL; 1457 } else { 1458 LERROR("dname()"); 1459 } 1460 break; 1461 default: 1462 LERROR("dname()"); 1463 } 1464 sym->s_scl = sc; 1465 1466 sym->s_type = dcs->d_type; 1467 1468 dcs->d_fpsyms = NULL; 1469 1470 return (sym); 1471} 1472 1473/* 1474 * Process a name in the list of formal params in an old style function 1475 * definition. 1476 */ 1477sym_t * 1478iname(sym_t *sym) 1479{ 1480 1481 if (sym->s_scl != NOSCL) { 1482 if (blklev == sym->s_blklev) { 1483 /* redeclaration of formal parameter %s */ 1484 error(21, sym->s_name); 1485 if (!sym->s_defarg) 1486 LERROR("iname()"); 1487 } 1488 sym = pushdown(sym); 1489 } 1490 sym->s_type = gettyp(INT); 1491 sym->s_scl = AUTO; 1492 sym->s_def = DEF; 1493 sym->s_defarg = sym->s_arg = 1; 1494 return (sym); 1495} 1496 1497/* 1498 * Create the type of a tag. 1499 * 1500 * tag points to the symbol table entry of the tag 1501 * kind is the kind of the tag (STRUCT/UNION/ENUM) 1502 * decl is 1 if the type of the tag will be completed in this declaration 1503 * (the following token is T_LBRACE) 1504 * semi is 1 if the following token is T_SEMI 1505 */ 1506type_t * 1507mktag(sym_t *tag, tspec_t kind, int decl, int semi) 1508{ 1509 scl_t scl = NOSCL; 1510 type_t *tp; 1511 1512 if (kind == STRUCT) { 1513 scl = STRTAG; 1514 } else if (kind == UNION) { 1515 scl = UNIONTAG; 1516 } else if (kind == ENUM) { 1517 scl = ENUMTAG; 1518 } else { 1519 LERROR("mktag()"); 1520 } 1521 1522 if (tag != NULL) { 1523 if (tag->s_scl != NOSCL) { 1524 tag = newtag(tag, scl, decl, semi); 1525 } else { 1526 /* a new tag, no empty declaration */ 1527 dcs->d_nxt->d_nedecl = 1; 1528 if (scl == ENUMTAG && !decl) { 1529 if (!tflag && (sflag || pflag)) 1530 /* forward reference to enum type */ 1531 warning(42); 1532 } 1533 } 1534 if (tag->s_scl == NOSCL) { 1535 tag->s_scl = scl; 1536 tag->s_type = tp = getblk(sizeof (type_t)); 1537 } else { 1538 tp = tag->s_type; 1539 } 1540 } else { 1541 tag = getblk(sizeof (sym_t)); 1542 tag->s_name = unnamed; 1543 UNIQUE_CURR_POS(tag->s_dpos); 1544 tag->s_kind = FTAG; 1545 tag->s_scl = scl; 1546 tag->s_blklev = -1; 1547 tag->s_type = tp = getblk(sizeof (type_t)); 1548 dcs->d_nxt->d_nedecl = 1; 1549 } 1550 1551 if (tp->t_tspec == NOTSPEC) { 1552 tp->t_tspec = kind; 1553 if (kind != ENUM) { 1554 tp->t_str = getblk(sizeof (str_t)); 1555 tp->t_str->align = CHAR_BIT; 1556 tp->t_str->stag = tag; 1557 } else { 1558 tp->t_isenum = 1; 1559 tp->t_enum = getblk(sizeof (enum_t)); 1560 tp->t_enum->etag = tag; 1561 } 1562 /* is incomplete type */ 1563 setcompl(tp, 1); 1564 } 1565 1566 return (tp); 1567} 1568 1569/* 1570 * Checks all possible cases of tag redeclarations. 1571 * decl is 1 if T_LBRACE follows 1572 * semi is 1 if T_SEMI follows 1573 */ 1574static sym_t * 1575newtag(sym_t *tag, scl_t scl, int decl, int semi) 1576{ 1577 1578 if (tag->s_blklev < blklev) { 1579 if (semi) { 1580 /* "struct a;" */ 1581 if (!tflag) { 1582 if (!sflag) 1583 /* decl. introduces new type ... */ 1584 warning(44, scltoa(scl), tag->s_name); 1585 tag = pushdown(tag); 1586 } else if (tag->s_scl != scl) { 1587 /* base type is really "%s %s" */ 1588 warning(45, scltoa(tag->s_scl), tag->s_name); 1589 } 1590 dcs->d_nxt->d_nedecl = 1; 1591 } else if (decl) { 1592 /* "struct a { ... } " */ 1593 if (hflag) 1594 /* redefinition hides earlier one: %s */ 1595 warning(43, tag->s_name); 1596 tag = pushdown(tag); 1597 dcs->d_nxt->d_nedecl = 1; 1598 } else if (tag->s_scl != scl) { 1599 /* base type is really "%s %s" */ 1600 warning(45, scltoa(tag->s_scl), tag->s_name); 1601 /* declaration introduces new type in ANSI C: %s %s */ 1602 if (!sflag) 1603 warning(44, scltoa(scl), tag->s_name); 1604 tag = pushdown(tag); 1605 dcs->d_nxt->d_nedecl = 1; 1606 } 1607 } else { 1608 if (tag->s_scl != scl) { 1609 /* (%s) tag redeclared */ 1610 error(46, scltoa(tag->s_scl)); 1611 prevdecl(-1, tag); 1612 tag = pushdown(tag); 1613 dcs->d_nxt->d_nedecl = 1; 1614 } else if (decl && !incompl(tag->s_type)) { 1615 /* (%s) tag redeclared */ 1616 error(46, scltoa(tag->s_scl)); 1617 prevdecl(-1, tag); 1618 tag = pushdown(tag); 1619 dcs->d_nxt->d_nedecl = 1; 1620 } else if (semi || decl) { 1621 dcs->d_nxt->d_nedecl = 1; 1622 } 1623 } 1624 return (tag); 1625} 1626 1627const char * 1628scltoa(scl_t sc) 1629{ 1630 const char *s; 1631 1632 switch (sc) { 1633 case EXTERN: s = "extern"; break; 1634 case STATIC: s = "static"; break; 1635 case AUTO: s = "auto"; break; 1636 case REG: s = "register"; break; 1637 case TYPEDEF: s = "typedef"; break; 1638 case STRTAG: s = "struct"; break; 1639 case UNIONTAG: s = "union"; break; 1640 case ENUMTAG: s = "enum"; break; 1641 default: LERROR("tagttoa()"); 1642 } 1643 return (s); 1644} 1645 1646/* 1647 * Completes the type of a tag in a struct/union/enum declaration. 1648 * tp points to the type of the, tag, fmem to the list of members/enums. 1649 */ 1650type_t * 1651compltag(type_t *tp, sym_t *fmem) 1652{ 1653 tspec_t t; 1654 str_t *sp; 1655 int n; 1656 sym_t *mem; 1657 1658 /* from now a complete type */ 1659 setcompl(tp, 0); 1660 1661 if ((t = tp->t_tspec) != ENUM) { 1662 align(dcs->d_stralign, 0); 1663 sp = tp->t_str; 1664 sp->align = dcs->d_stralign; 1665 sp->size = dcs->d_offset; 1666 sp->memb = fmem; 1667 if (sp->size == 0) { 1668 /* zero sized %s */ 1669 (void)c99ism(47, ttab[t].tt_name); 1670 } else { 1671 n = 0; 1672 for (mem = fmem; mem != NULL; mem = mem->s_nxt) { 1673 if (mem->s_name != unnamed) 1674 n++; 1675 } 1676 if (n == 0) { 1677 /* %s has no named members */ 1678 warning(65, 1679 t == STRUCT ? "structure" : "union"); 1680 } 1681 } 1682 } else { 1683 tp->t_enum->elem = fmem; 1684 } 1685 return (tp); 1686} 1687 1688/* 1689 * Processes the name of an enumerator in en enum declaration. 1690 * 1691 * sym points to the enumerator 1692 * val is the value of the enumerator 1693 * impl is 1 if the value of the enumerator was not explicit specified. 1694 */ 1695sym_t * 1696ename(sym_t *sym, int val, int impl) 1697{ 1698 1699 if (sym->s_scl) { 1700 if (sym->s_blklev == blklev) { 1701 /* no hflag, because this is illegal!!! */ 1702 if (sym->s_arg) { 1703 /* enumeration constant hides parameter: %s */ 1704 warning(57, sym->s_name); 1705 } else { 1706 /* redeclaration of %s */ 1707 error(27, sym->s_name); 1708 /* 1709 * inside blocks it should not too complicated 1710 * to find the position of the previous 1711 * declaration 1712 */ 1713 if (blklev == 0) 1714 prevdecl(-1, sym); 1715 } 1716 } else { 1717 if (hflag) 1718 /* redefinition hides earlier one: %s */ 1719 warning(43, sym->s_name); 1720 } 1721 sym = pushdown(sym); 1722 } 1723 sym->s_scl = ENUMCON; 1724 sym->s_type = dcs->d_tagtyp; 1725 sym->s_value.v_tspec = INT; 1726 sym->s_value.v_quad = val; 1727 if (impl && val - 1 == INT_MAX) { 1728 /* overflow in enumeration values: %s */ 1729 warning(48, sym->s_name); 1730 } 1731 enumval = val + 1; 1732 return (sym); 1733} 1734 1735/* 1736 * Process a single external declarator. 1737 */ 1738void 1739decl1ext(sym_t *dsym, int initflg) 1740{ 1741 int warn, rval, redec; 1742 sym_t *rdsym; 1743 1744 chkfdef(dsym, 1); 1745 1746 chktyp(dsym); 1747 1748 if (initflg && !(initerr = chkinit(dsym))) 1749 dsym->s_def = DEF; 1750 1751 /* 1752 * Declarations of functions are marked as "tentative" in dname(). 1753 * This is wrong because there are no tentative function 1754 * definitions. 1755 */ 1756 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF) 1757 dsym->s_def = DECL; 1758 1759 if (dcs->d_inline) { 1760 if (dsym->s_type->t_tspec == FUNC) { 1761 dsym->s_inline = 1; 1762 } else { 1763 /* variable declared inline: %s */ 1764 warning(268, dsym->s_name); 1765 } 1766 } 1767 1768 /* Write the declaration into the output file */ 1769 if (plibflg && llibflg && 1770 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) { 1771 /* 1772 * With both LINTLIBRARY and PROTOLIB the prototype is 1773 * written as a function definition to the output file. 1774 */ 1775 rval = dsym->s_type->t_subt->t_tspec != VOID; 1776 outfdef(dsym, &dsym->s_dpos, rval, 0, NULL); 1777 } else { 1778 outsym(dsym, dsym->s_scl, dsym->s_def); 1779 } 1780 1781 if ((rdsym = dcs->d_rdcsym) != NULL) { 1782 1783 /* 1784 * If the old symbol stems from an old style function definition 1785 * we have remembered the params in rdsmy->s_args and compare 1786 * them with the params of the prototype. 1787 */ 1788 if (rdsym->s_osdef && dsym->s_type->t_proto) { 1789 redec = chkosdef(rdsym, dsym); 1790 } else { 1791 redec = 0; 1792 } 1793 1794 if (!redec && !isredec(dsym, (warn = 0, &warn))) { 1795 1796 if (warn) { 1797 /* redeclaration of %s */ 1798 (*(sflag ? error : warning))(27, dsym->s_name); 1799 prevdecl(-1, rdsym); 1800 } 1801 1802 /* 1803 * Overtake the remembered params if the new symbol 1804 * is not a prototype. 1805 */ 1806 if (rdsym->s_osdef && !dsym->s_type->t_proto) { 1807 dsym->s_osdef = rdsym->s_osdef; 1808 dsym->s_args = rdsym->s_args; 1809 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos); 1810 } 1811 1812 /* 1813 * Remember the position of the declaration if the 1814 * old symbol was a prototype and the new is not. 1815 * Also remember the position if the old symbol 1816 * was defined and the new is not. 1817 */ 1818 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) { 1819 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos); 1820 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) { 1821 STRUCT_ASSIGN(dsym->s_dpos, rdsym->s_dpos); 1822 } 1823 1824 /* 1825 * Copy informations about usage of the name into 1826 * the new symbol. 1827 */ 1828 cpuinfo(dsym, rdsym); 1829 1830 /* Once a name is defined, it remains defined. */ 1831 if (rdsym->s_def == DEF) 1832 dsym->s_def = DEF; 1833 1834 /* once a function is inline, it remains inline */ 1835 if (rdsym->s_inline) 1836 dsym->s_inline = 1; 1837 1838 compltyp(dsym, rdsym); 1839 1840 } 1841 1842 rmsym(rdsym); 1843 } 1844 1845 if (dsym->s_scl == TYPEDEF) { 1846 dsym->s_type = duptyp(dsym->s_type); 1847 dsym->s_type->t_typedef = 1; 1848 settdsym(dsym->s_type, dsym); 1849 } 1850 1851} 1852 1853/* 1854 * Copies informations about usage into a new symbol table entry of 1855 * the same symbol. 1856 */ 1857void 1858cpuinfo(sym_t *sym, sym_t *rdsym) 1859{ 1860 1861 sym->s_spos = rdsym->s_spos; 1862 sym->s_upos = rdsym->s_upos; 1863 sym->s_set = rdsym->s_set; 1864 sym->s_used = rdsym->s_used; 1865} 1866 1867/* 1868 * Prints an error and returns 1 if a symbol is redeclared/redefined. 1869 * Otherwise returns 0 and, in some cases of minor problems, prints 1870 * a warning. 1871 */ 1872int 1873isredec(sym_t *dsym, int *warn) 1874{ 1875 sym_t *rsym; 1876 1877 if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) { 1878 /* redeclaration of %s */ 1879 error(27, dsym->s_name); 1880 prevdecl(-1, rsym); 1881 return (1); 1882 } 1883 if (rsym->s_scl == TYPEDEF) { 1884 /* typedef redeclared: %s */ 1885 error(89, dsym->s_name); 1886 prevdecl(-1, rsym); 1887 return (1); 1888 } 1889 if (dsym->s_scl == TYPEDEF) { 1890 /* redeclaration of %s */ 1891 error(27, dsym->s_name); 1892 prevdecl(-1, rsym); 1893 return (1); 1894 } 1895 if (rsym->s_def == DEF && dsym->s_def == DEF) { 1896 /* redefinition of %s */ 1897 error(28, dsym->s_name); 1898 prevdecl(-1, rsym); 1899 return(1); 1900 } 1901 if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, warn)) { 1902 /* redeclaration of %s */ 1903 error(27, dsym->s_name); 1904 prevdecl(-1, rsym); 1905 return(1); 1906 } 1907 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN) 1908 return(0); 1909 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC) 1910 return(0); 1911 if (rsym->s_scl == STATIC && dsym->s_def == DECL) 1912 return(0); 1913 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) { 1914 /* 1915 * All cases except "int a = 1; static int a;" are caught 1916 * above with or without a warning 1917 */ 1918 /* redeclaration of %s */ 1919 error(27, dsym->s_name); 1920 prevdecl(-1, rsym); 1921 return(1); 1922 } 1923 if (rsym->s_scl == EXTERN) { 1924 /* previously declared extern, becomes static: %s */ 1925 warning(29, dsym->s_name); 1926 prevdecl(-1, rsym); 1927 return(0); 1928 } 1929 /* 1930 * Now its on of: 1931 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;" 1932 */ 1933 /* redeclaration of %s; ANSI C requires "static" */ 1934 if (sflag) { 1935 warning(30, dsym->s_name); 1936 prevdecl(-1, rsym); 1937 } 1938 dsym->s_scl = STATIC; 1939 return (0); 1940} 1941 1942/* 1943 * Checks if two types are compatible. Returns 0 if not, otherwise 1. 1944 * 1945 * ignqual ignore qualifiers of type; used for function params 1946 * promot promote left type; used for comparison of params of 1947 * old style function definitions with params of prototypes. 1948 * *warn set to 1 if an old style function declaration is not 1949 * compatible with a prototype 1950 */ 1951int 1952eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *warn) 1953{ 1954 tspec_t t; 1955 1956 while (tp1 != NULL && tp2 != NULL) { 1957 1958 t = tp1->t_tspec; 1959 if (promot) { 1960 if (t == FLOAT) { 1961 t = DOUBLE; 1962 } else if (t == CHAR || t == SCHAR) { 1963 t = INT; 1964 } else if (t == UCHAR) { 1965 t = tflag ? UINT : INT; 1966 } else if (t == SHORT) { 1967 t = INT; 1968 } else if (t == USHORT) { 1969 /* CONSTCOND */ 1970 t = INT_MAX < USHRT_MAX || tflag ? UINT : INT; 1971 } 1972 } 1973 1974 if (t != tp2->t_tspec) 1975 return (0); 1976 1977 if (tp1->t_const != tp2->t_const && !ignqual && !tflag) 1978 return (0); 1979 1980 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag) 1981 return (0); 1982 1983 if (t == STRUCT || t == UNION) 1984 return (tp1->t_str == tp2->t_str); 1985 1986 if (t == ARRAY && tp1->t_dim != tp2->t_dim) { 1987 if (tp1->t_dim != 0 && tp2->t_dim != 0) 1988 return (0); 1989 } 1990 1991 /* dont check prototypes for traditional */ 1992 if (t == FUNC && !tflag) { 1993 if (tp1->t_proto && tp2->t_proto) { 1994 if (!eqargs(tp1, tp2, warn)) 1995 return (0); 1996 } else if (tp1->t_proto) { 1997 if (!mnoarg(tp1, warn)) 1998 return (0); 1999 } else if (tp2->t_proto) { 2000 if (!mnoarg(tp2, warn)) 2001 return (0); 2002 } 2003 } 2004 2005 tp1 = tp1->t_subt; 2006 tp2 = tp2->t_subt; 2007 ignqual = promot = 0; 2008 2009 } 2010 2011 return (tp1 == tp2); 2012} 2013 2014/* 2015 * Compares the parameter types of two prototypes. 2016 */ 2017static int 2018eqargs(type_t *tp1, type_t *tp2, int *warn) 2019{ 2020 sym_t *a1, *a2; 2021 2022 if (tp1->t_vararg != tp2->t_vararg) 2023 return (0); 2024 2025 a1 = tp1->t_args; 2026 a2 = tp2->t_args; 2027 2028 while (a1 != NULL && a2 != NULL) { 2029 2030 if (eqtype(a1->s_type, a2->s_type, 1, 0, warn) == 0) 2031 return (0); 2032 2033 a1 = a1->s_nxt; 2034 a2 = a2->s_nxt; 2035 2036 } 2037 2038 return (a1 == a2); 2039} 2040 2041/* 2042 * mnoarg() (matches functions with no argument type information) 2043 * returns 1 if all parameters of a prototype are compatible with 2044 * and old style function declaration. 2045 * This is the case if following conditions are met: 2046 * 1. the prototype must have a fixed number of parameters 2047 * 2. no parameter is of type float 2048 * 3. no parameter is converted to another type if integer promotion 2049 * is applied on it 2050 */ 2051static int 2052mnoarg(type_t *tp, int *warn) 2053{ 2054 sym_t *arg; 2055 tspec_t t; 2056 2057 if (tp->t_vararg) { 2058 if (warn != NULL) 2059 *warn = 1; 2060 } 2061 for (arg = tp->t_args; arg != NULL; arg = arg->s_nxt) { 2062 if ((t = arg->s_type->t_tspec) == FLOAT || 2063 t == CHAR || t == SCHAR || t == UCHAR || 2064 t == SHORT || t == USHORT) { 2065 if (warn != NULL) 2066 *warn = 1; 2067 } 2068 } 2069 return (1); 2070} 2071 2072/* 2073 * Compares a prototype declaration with the remembered arguments of 2074 * a previous old style function definition. 2075 */ 2076static int 2077chkosdef(sym_t *rdsym, sym_t *dsym) 2078{ 2079 sym_t *args, *pargs, *arg, *parg; 2080 int narg, nparg, n; 2081 int warn, msg; 2082 2083 args = rdsym->s_args; 2084 pargs = dsym->s_type->t_args; 2085 2086 msg = 0; 2087 2088 narg = nparg = 0; 2089 for (arg = args; arg != NULL; arg = arg->s_nxt) 2090 narg++; 2091 for (parg = pargs; parg != NULL; parg = parg->s_nxt) 2092 nparg++; 2093 if (narg != nparg) { 2094 /* prototype does not match old-style definition */ 2095 error(63); 2096 msg = 1; 2097 goto end; 2098 } 2099 2100 arg = args; 2101 parg = pargs; 2102 n = 1; 2103 while (narg--) { 2104 warn = 0; 2105 /* 2106 * If it does not match due to promotion and sflag is 2107 * not set we print only a warning. 2108 */ 2109 if (!eqtype(arg->s_type, parg->s_type, 1, 1, &warn) || warn) { 2110 /* prototype does not match old-style def., arg #%d */ 2111 error(299, n); 2112 msg = 1; 2113 } 2114 arg = arg->s_nxt; 2115 parg = parg->s_nxt; 2116 n++; 2117 } 2118 2119 end: 2120 if (msg) 2121 /* old style definition */ 2122 prevdecl(300, rdsym); 2123 2124 return (msg); 2125} 2126 2127/* 2128 * Completes a type by copying the dimension and prototype information 2129 * from a second compatible type. 2130 * 2131 * Following lines are legal: 2132 * "typedef a[]; a b; a b[10]; a c; a c[20];" 2133 * "typedef ft(); ft f; f(int); ft g; g(long);" 2134 * This means that, if a type is completed, the type structure must 2135 * be duplicated. 2136 */ 2137void 2138compltyp(sym_t *dsym, sym_t *ssym) 2139{ 2140 type_t **dstp, *src; 2141 type_t *dst; 2142 2143 dstp = &dsym->s_type; 2144 src = ssym->s_type; 2145 2146 while ((dst = *dstp) != NULL) { 2147 if (src == NULL || dst->t_tspec != src->t_tspec) 2148 LERROR("compltyp()"); 2149 if (dst->t_tspec == ARRAY) { 2150 if (dst->t_dim == 0 && src->t_dim != 0) { 2151 *dstp = dst = duptyp(dst); 2152 dst->t_dim = src->t_dim; 2153 /* now a complete type */ 2154 setcompl(dst, 0); 2155 } 2156 } else if (dst->t_tspec == FUNC) { 2157 if (!dst->t_proto && src->t_proto) { 2158 *dstp = dst = duptyp(dst); 2159 dst->t_proto = 1; 2160 dst->t_args = src->t_args; 2161 } 2162 } 2163 dstp = &dst->t_subt; 2164 src = src->t_subt; 2165 } 2166} 2167 2168/* 2169 * Completes the declaration of a single argument. 2170 */ 2171sym_t * 2172decl1arg(sym_t *sym, int initflg) 2173{ 2174 tspec_t t; 2175 2176 chkfdef(sym, 1); 2177 2178 chktyp(sym); 2179 2180 if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) { 2181 /* redeclaration of formal parameter %s */ 2182 error(237, sym->s_name); 2183 rmsym(dcs->d_rdcsym); 2184 sym->s_arg = 1; 2185 } 2186 2187 if (!sym->s_arg) { 2188 /* declared argument %s is missing */ 2189 error(53, sym->s_name); 2190 sym->s_arg = 1; 2191 } 2192 2193 if (initflg) { 2194 /* cannot initialize parameter: %s */ 2195 error(52, sym->s_name); 2196 initerr = 1; 2197 } 2198 2199 if ((t = sym->s_type->t_tspec) == ARRAY) { 2200 sym->s_type = incref(sym->s_type->t_subt, PTR); 2201 } else if (t == FUNC) { 2202 if (tflag) 2203 /* a function is declared as an argument: %s */ 2204 warning(50, sym->s_name); 2205 sym->s_type = incref(sym->s_type, PTR); 2206 } else if (t == FLOAT) { 2207 if (tflag) 2208 sym->s_type = gettyp(DOUBLE); 2209 } 2210 2211 if (dcs->d_inline) 2212 /* argument declared inline: %s */ 2213 warning(269, sym->s_name); 2214 2215 /* 2216 * Arguments must have complete types. lengths() prints the needed 2217 * error messages (null dimension is impossible because arrays are 2218 * converted to pointers). 2219 */ 2220 if (sym->s_type->t_tspec != VOID) 2221 (void)length(sym->s_type, sym->s_name); 2222 2223 setsflg(sym); 2224 2225 return (sym); 2226} 2227 2228/* 2229 * Does some checks for lint directives which apply to functions. 2230 * Processes arguments in old style function definitions which default 2231 * to int. 2232 * Checks compatibility of old style function definition with previous 2233 * prototype. 2234 */ 2235void 2236cluparg(void) 2237{ 2238 sym_t *args, *arg, *pargs, *parg; 2239 int narg, nparg, n, msg; 2240 tspec_t t; 2241 2242 args = funcsym->s_args; 2243 pargs = funcsym->s_type->t_args; 2244 2245 /* check for illegal combinations of lint directives */ 2246 if (prflstrg != -1 && scflstrg != -1) { 2247 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */ 2248 warning(289); 2249 prflstrg = scflstrg = -1; 2250 } 2251 if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) { 2252 /* dubious use of ** VARARGS ** with ** %s ** */ 2253 warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE"); 2254 nvararg = -1; 2255 } 2256 2257 /* 2258 * check if the argument of a lint directive is compatible with the 2259 * number of arguments. 2260 */ 2261 narg = 0; 2262 for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_nxt) 2263 narg++; 2264 if (nargusg > narg) { 2265 /* argument number mismatch with directive: ** %s ** */ 2266 warning(283, "ARGSUSED"); 2267 nargusg = 0; 2268 } 2269 if (nvararg > narg) { 2270 /* argument number mismatch with directive: ** %s ** */ 2271 warning(283, "VARARGS"); 2272 nvararg = 0; 2273 } 2274 if (prflstrg > narg) { 2275 /* argument number mismatch with directive: ** %s ** */ 2276 warning(283, "PRINTFLIKE"); 2277 prflstrg = -1; 2278 } else if (prflstrg == 0) { 2279 prflstrg = -1; 2280 } 2281 if (scflstrg > narg) { 2282 /* argument number mismatch with directive: ** %s ** */ 2283 warning(283, "SCANFLIKE"); 2284 scflstrg = -1; 2285 } else if (scflstrg == 0) { 2286 scflstrg = -1; 2287 } 2288 if (prflstrg != -1 || scflstrg != -1) { 2289 narg = prflstrg != -1 ? prflstrg : scflstrg; 2290 arg = dcs->d_fargs; 2291 for (n = 1; n < narg; n++) 2292 arg = arg->s_nxt; 2293 if (arg->s_type->t_tspec != PTR || 2294 ((t = arg->s_type->t_subt->t_tspec) != CHAR && 2295 t != UCHAR && t != SCHAR)) { 2296 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */ 2297 warning(293, narg); 2298 prflstrg = scflstrg = -1; 2299 } 2300 } 2301 2302 /* 2303 * print a warning for each argument of an old style function 2304 * definition which defaults to int 2305 */ 2306 for (arg = args; arg != NULL; arg = arg->s_nxt) { 2307 if (arg->s_defarg) { 2308 /* argument type defaults to int: %s */ 2309 warning(32, arg->s_name); 2310 arg->s_defarg = 0; 2311 setsflg(arg); 2312 } 2313 } 2314 2315 /* 2316 * If this is an old style function definition and a prototyp 2317 * exists, compare the types of arguments. 2318 */ 2319 if (funcsym->s_osdef && funcsym->s_type->t_proto) { 2320 /* 2321 * If the number of arguments does not macht, we need not 2322 * continue. 2323 */ 2324 narg = nparg = 0; 2325 msg = 0; 2326 for (parg = pargs; parg != NULL; parg = parg->s_nxt) 2327 nparg++; 2328 for (arg = args; arg != NULL; arg = arg->s_nxt) 2329 narg++; 2330 if (narg != nparg) { 2331 /* parameter mismatch: %d declared, %d defined */ 2332 error(51, nparg, narg); 2333 msg = 1; 2334 } else { 2335 parg = pargs; 2336 arg = args; 2337 while (narg--) { 2338 msg |= chkptdecl(arg, parg); 2339 parg = parg->s_nxt; 2340 arg = arg->s_nxt; 2341 } 2342 } 2343 if (msg) 2344 /* prototype declaration */ 2345 prevdecl(285, dcs->d_rdcsym); 2346 2347 /* from now the prototype is valid */ 2348 funcsym->s_osdef = 0; 2349 funcsym->s_args = NULL; 2350 2351 } 2352 2353} 2354 2355/* 2356 * Checks compatibility of an old style function definition with a previous 2357 * prototype declaration. 2358 * Returns 1 if the position of the previous declaration should be reported. 2359 */ 2360static int 2361chkptdecl(sym_t *arg, sym_t *parg) 2362{ 2363 type_t *tp, *ptp; 2364 int warn, msg; 2365 2366 tp = arg->s_type; 2367 ptp = parg->s_type; 2368 2369 msg = 0; 2370 warn = 0; 2371 2372 if (!eqtype(tp, ptp, 1, 1, &warn)) { 2373 if (eqtype(tp, ptp, 1, 0, &warn)) { 2374 /* type does not match prototype: %s */ 2375 msg = gnuism(58, arg->s_name); 2376 } else { 2377 /* type does not match prototype: %s */ 2378 error(58, arg->s_name); 2379 msg = 1; 2380 } 2381 } else if (warn) { 2382 /* type does not match prototype: %s */ 2383 (*(sflag ? error : warning))(58, arg->s_name); 2384 msg = 1; 2385 } 2386 2387 return (msg); 2388} 2389 2390/* 2391 * Completes a single local declaration/definition. 2392 */ 2393void 2394decl1loc(sym_t *dsym, int initflg) 2395{ 2396 2397 /* Correct a mistake done in dname(). */ 2398 if (dsym->s_type->t_tspec == FUNC) { 2399 dsym->s_def = DECL; 2400 if (dcs->d_scl == NOSCL) 2401 dsym->s_scl = EXTERN; 2402 } 2403 2404 if (dsym->s_type->t_tspec == FUNC) { 2405 if (dsym->s_scl == STATIC) { 2406 /* dubious static function at block level: %s */ 2407 warning(93, dsym->s_name); 2408 dsym->s_scl = EXTERN; 2409 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) { 2410 /* function has illegal storage class: %s */ 2411 error(94, dsym->s_name); 2412 dsym->s_scl = EXTERN; 2413 } 2414 } 2415 2416 /* 2417 * functions may be declared inline at local scope, although 2418 * this has no effect for a later definition of the same 2419 * function. 2420 * XXX it should have an effect if tflag is set. this would 2421 * also be the way gcc behaves. 2422 */ 2423 if (dcs->d_inline) { 2424 if (dsym->s_type->t_tspec == FUNC) { 2425 dsym->s_inline = 1; 2426 } else { 2427 /* variable declared inline: %s */ 2428 warning(268, dsym->s_name); 2429 } 2430 } 2431 2432 chkfdef(dsym, 1); 2433 2434 chktyp(dsym); 2435 2436 if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN) 2437 ledecl(dsym); 2438 2439 if (dsym->s_scl == EXTERN) { 2440 /* 2441 * XXX wenn die statische Variable auf Ebene 0 erst 2442 * spaeter definiert wird, haben wir die Brille auf. 2443 */ 2444 if (dsym->s_xsym == NULL) { 2445 outsym(dsym, EXTERN, dsym->s_def); 2446 } else { 2447 outsym(dsym, dsym->s_xsym->s_scl, dsym->s_def); 2448 } 2449 } 2450 2451 if (dcs->d_rdcsym != NULL) { 2452 2453 if (dcs->d_rdcsym->s_blklev == 0) { 2454 2455 switch (dsym->s_scl) { 2456 case AUTO: 2457 /* automatic hides external declaration: %s */ 2458 if (hflag) 2459 warning(86, dsym->s_name); 2460 break; 2461 case STATIC: 2462 /* static hides external declaration: %s */ 2463 if (hflag) 2464 warning(87, dsym->s_name); 2465 break; 2466 case TYPEDEF: 2467 /* typedef hides external declaration: %s */ 2468 if (hflag) 2469 warning(88, dsym->s_name); 2470 break; 2471 case EXTERN: 2472 /* 2473 * Warnings and errors are printed in ledecl() 2474 */ 2475 break; 2476 default: 2477 LERROR("decl1loc()"); 2478 } 2479 2480 } else if (dcs->d_rdcsym->s_blklev == blklev) { 2481 2482 /* no hflag, because its illegal! */ 2483 if (dcs->d_rdcsym->s_arg) { 2484 /* 2485 * if !tflag, a "redeclaration of %s" error 2486 * is produced below 2487 */ 2488 if (tflag) { 2489 if (hflag) 2490 /* decl. hides parameter: %s */ 2491 warning(91, dsym->s_name); 2492 rmsym(dcs->d_rdcsym); 2493 } 2494 } 2495 2496 } else if (dcs->d_rdcsym->s_blklev < blklev) { 2497 2498 if (hflag) 2499 /* declaration hides earlier one: %s */ 2500 warning(95, dsym->s_name); 2501 2502 } 2503 2504 if (dcs->d_rdcsym->s_blklev == blklev) { 2505 2506 /* redeclaration of %s */ 2507 error(27, dsym->s_name); 2508 rmsym(dcs->d_rdcsym); 2509 2510 } 2511 2512 } 2513 2514 if (initflg && !(initerr = chkinit(dsym))) { 2515 dsym->s_def = DEF; 2516 setsflg(dsym); 2517 } 2518 2519 if (dsym->s_scl == TYPEDEF) { 2520 dsym->s_type = duptyp(dsym->s_type); 2521 dsym->s_type->t_typedef = 1; 2522 settdsym(dsym->s_type, dsym); 2523 } 2524 2525 /* 2526 * Before we can check the size we must wait for an initialisation 2527 * which may follow. 2528 */ 2529} 2530 2531/* 2532 * Processes (re)declarations of external Symbols inside blocks. 2533 */ 2534static void 2535ledecl(sym_t *dsym) 2536{ 2537 int eqt, warn; 2538 sym_t *esym; 2539 2540 /* look for a symbol with the same name */ 2541 esym = dcs->d_rdcsym; 2542 while (esym != NULL && esym->s_blklev != 0) { 2543 while ((esym = esym->s_link) != NULL) { 2544 if (esym->s_kind != FVFT) 2545 continue; 2546 if (strcmp(dsym->s_name, esym->s_name) == 0) 2547 break; 2548 } 2549 } 2550 if (esym == NULL) 2551 return; 2552 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) { 2553 /* gcc accepts this without a warning, pcc prints an error. */ 2554 /* redeclaration of %s */ 2555 warning(27, dsym->s_name); 2556 prevdecl(-1, esym); 2557 return; 2558 } 2559 2560 warn = 0; 2561 eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &warn); 2562 2563 if (!eqt || warn) { 2564 if (esym->s_scl == EXTERN) { 2565 /* inconsistent redeclaration of extern: %s */ 2566 warning(90, dsym->s_name); 2567 prevdecl(-1, esym); 2568 } else { 2569 /* inconsistent redeclaration of static: %s */ 2570 warning(92, dsym->s_name); 2571 prevdecl(-1, esym); 2572 } 2573 } 2574 2575 if (eqt) { 2576 /* 2577 * Remember the external symbol so we can update usage 2578 * information at the end of the block. 2579 */ 2580 dsym->s_xsym = esym; 2581 } 2582} 2583 2584/* 2585 * Print an error or a warning if the symbol can't be initialized due 2586 * to type/storage class. Return value is 1 if an error has been 2587 * detected. 2588 */ 2589static int 2590chkinit(sym_t *sym) 2591{ 2592 int err; 2593 2594 err = 0; 2595 2596 if (sym->s_type->t_tspec == FUNC) { 2597 /* cannot initialize function: %s */ 2598 error(24, sym->s_name); 2599 err = 1; 2600 } else if (sym->s_scl == TYPEDEF) { 2601 /* cannot initialize typedef: %s */ 2602 error(25, sym->s_name); 2603 err = 1; 2604 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) { 2605 /* cannot initialize "extern" declaration: %s */ 2606 if (dcs->d_ctx == EXTERN) { 2607 warning(26, sym->s_name); 2608 } else { 2609 error(26, sym->s_name); 2610 err = 1; 2611 } 2612 } 2613 2614 return (err); 2615} 2616 2617/* 2618 * Create a symbol for an abstract declaration. 2619 */ 2620sym_t * 2621aname(void) 2622{ 2623 sym_t *sym; 2624 2625 if (dcs->d_ctx != ABSTRACT && dcs->d_ctx != PARG) 2626 LERROR("aname()"); 2627 2628 sym = getblk(sizeof (sym_t)); 2629 2630 sym->s_name = unnamed; 2631 sym->s_def = DEF; 2632 sym->s_scl = ABSTRACT; 2633 sym->s_blklev = -1; 2634 2635 if (dcs->d_ctx == PARG) 2636 sym->s_arg = 1; 2637 2638 sym->s_type = dcs->d_type; 2639 dcs->d_rdcsym = NULL; 2640 dcs->d_vararg = 0; 2641 2642 return (sym); 2643} 2644 2645/* 2646 * Removes anything which has nothing to do on global level. 2647 */ 2648void 2649globclup(void) 2650{ 2651 2652 while (dcs->d_nxt != NULL) 2653 popdecl(); 2654 2655 cleanup(); 2656 blklev = 0; 2657 mblklev = 0; 2658 2659 /* 2660 * remove all information about pending lint directives without 2661 * warnings. 2662 */ 2663 glclup(1); 2664} 2665 2666/* 2667 * Process an abstract type declaration 2668 */ 2669sym_t * 2670decl1abs(sym_t *sym) 2671{ 2672 2673 chkfdef(sym, 1); 2674 chktyp(sym); 2675 return (sym); 2676} 2677 2678/* 2679 * Checks size after declarations of variables and their initialisation. 2680 */ 2681void 2682chksz(sym_t *dsym) 2683{ 2684 2685 /* 2686 * check size only for symbols which are defined and no function and 2687 * not typedef name 2688 */ 2689 if (dsym->s_def != DEF) 2690 return; 2691 if (dsym->s_scl == TYPEDEF) 2692 return; 2693 if (dsym->s_type->t_tspec == FUNC) 2694 return; 2695 2696 if (length(dsym->s_type, dsym->s_name) == 0 && 2697 dsym->s_type->t_tspec == ARRAY && dsym->s_type->t_dim == 0) { 2698 /* empty array declaration: %s */ 2699 if (tflag) { 2700 warning(190, dsym->s_name); 2701 } else { 2702 error(190, dsym->s_name); 2703 } 2704 } 2705} 2706 2707/* 2708 * Mark an object as set if it is not already 2709 */ 2710void 2711setsflg(sym_t *sym) 2712{ 2713 2714 if (!sym->s_set) { 2715 sym->s_set = 1; 2716 UNIQUE_CURR_POS(sym->s_spos); 2717 } 2718} 2719 2720/* 2721 * Mark an object as used if it is not already 2722 */ 2723void 2724setuflg(sym_t *sym, int fcall, int szof) 2725{ 2726 2727 if (!sym->s_used) { 2728 sym->s_used = 1; 2729 UNIQUE_CURR_POS(sym->s_upos); 2730 } 2731 /* 2732 * for function calls another record is written 2733 * 2734 * XXX Should symbols used in sizeof() treated as used or not? 2735 * Probably not, because there is no sense to declare an 2736 * external variable only to get their size. 2737 */ 2738 if (!fcall && !szof && sym->s_kind == FVFT && sym->s_scl == EXTERN) 2739 outusg(sym); 2740} 2741 2742/* 2743 * Prints warnings for a list of variables and labels (concatenated 2744 * with s_dlnxt) if these are not used or only set. 2745 */ 2746void 2747chkusage(dinfo_t *di) 2748{ 2749 sym_t *sym; 2750 int mknowarn; 2751 2752 /* for this warnings LINTED has no effect */ 2753 mknowarn = nowarn; 2754 nowarn = 0; 2755 2756 for (sym = di->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) 2757 chkusg1(di->d_asm, sym); 2758 2759 nowarn = mknowarn; 2760} 2761 2762/* 2763 * Prints a warning for a single variable or label if it is not used or 2764 * only set. 2765 */ 2766void 2767chkusg1(int novar, sym_t *sym) 2768{ 2769 pos_t cpos; 2770 2771 if (sym->s_blklev == -1) 2772 return; 2773 2774 STRUCT_ASSIGN(cpos, curr_pos); 2775 2776 if (sym->s_kind == FVFT) { 2777 if (sym->s_arg) { 2778 chkausg(novar, sym); 2779 } else { 2780 chkvusg(novar, sym); 2781 } 2782 } else if (sym->s_kind == FLAB) { 2783 chklusg(sym); 2784 } else if (sym->s_kind == FTAG) { 2785 chktusg(sym); 2786 } 2787 2788 STRUCT_ASSIGN(curr_pos, cpos); 2789} 2790 2791static void 2792chkausg(int novar, sym_t *arg) 2793{ 2794 2795 if (!arg->s_set) 2796 LERROR("chkausg()"); 2797 2798 if (novar) 2799 return; 2800 2801 if (!arg->s_used && vflag) { 2802 STRUCT_ASSIGN(curr_pos, arg->s_dpos); 2803 /* argument %s unused in function %s */ 2804 warning(231, arg->s_name, funcsym->s_name); 2805 } 2806} 2807 2808static void 2809chkvusg(int novar, sym_t *sym) 2810{ 2811 scl_t sc; 2812 sym_t *xsym; 2813 2814 if (blklev == 0 || sym->s_blklev == 0) 2815 LERROR("chkvusg()"); 2816 2817 /* errors in expressions easily cause lots of these warnings */ 2818 if (nerr != 0) 2819 return; 2820 2821 /* 2822 * XXX Only variables are checkd, although types should 2823 * probably also be checked 2824 */ 2825 if ((sc = sym->s_scl) != EXTERN && sc != STATIC && 2826 sc != AUTO && sc != REG) { 2827 return; 2828 } 2829 2830 if (novar) 2831 return; 2832 2833 if (sc == EXTERN) { 2834 if (!sym->s_used && !sym->s_set) { 2835 STRUCT_ASSIGN(curr_pos, sym->s_dpos); 2836 /* %s unused in function %s */ 2837 warning(192, sym->s_name, funcsym->s_name); 2838 } 2839 } else { 2840 if (sym->s_set && !sym->s_used) { 2841 STRUCT_ASSIGN(curr_pos, sym->s_spos); 2842 /* %s set but not used in function %s */ 2843 warning(191, sym->s_name, funcsym->s_name); 2844 } else if (!sym->s_used) { 2845 STRUCT_ASSIGN(curr_pos, sym->s_dpos); 2846 /* %s unused in function %s */ 2847 warning(192, sym->s_name, funcsym->s_name); 2848 } 2849 } 2850 2851 if (sc == EXTERN) { 2852 /* 2853 * information about usage is taken over into the symbol 2854 * tabel entry at level 0 if the symbol was locally declared 2855 * as an external symbol. 2856 * 2857 * XXX This is wrong for symbols declared static at level 0 2858 * if the usage information stems from sizeof(). This is 2859 * because symbols at level 0 only used in sizeof() are 2860 * considered to not be used. 2861 */ 2862 if ((xsym = sym->s_xsym) != NULL) { 2863 if (sym->s_used && !xsym->s_used) { 2864 xsym->s_used = 1; 2865 STRUCT_ASSIGN(xsym->s_upos, sym->s_upos); 2866 } 2867 if (sym->s_set && !xsym->s_set) { 2868 xsym->s_set = 1; 2869 STRUCT_ASSIGN(xsym->s_spos, sym->s_spos); 2870 } 2871 } 2872 } 2873} 2874 2875static void 2876chklusg(sym_t *lab) 2877{ 2878 2879 if (blklev != 1 || lab->s_blklev != 1) 2880 LERROR("chklusg()"); 2881 2882 if (lab->s_set && !lab->s_used) { 2883 STRUCT_ASSIGN(curr_pos, lab->s_spos); 2884 /* label %s unused in function %s */ 2885 warning(192, lab->s_name, funcsym->s_name); 2886 } else if (!lab->s_set) { 2887 STRUCT_ASSIGN(curr_pos, lab->s_upos); 2888 /* undefined label %s */ 2889 warning(23, lab->s_name); 2890 } 2891} 2892 2893static void 2894chktusg(sym_t *sym) 2895{ 2896 2897 if (!incompl(sym->s_type)) 2898 return; 2899 2900 /* complain always about incomplete tags declared inside blocks */ 2901 if (!zflag || dcs->d_ctx != EXTERN) 2902 return; 2903 2904 STRUCT_ASSIGN(curr_pos, sym->s_dpos); 2905 switch (sym->s_type->t_tspec) { 2906 case STRUCT: 2907 /* struct %s never defined */ 2908 warning(233, sym->s_name); 2909 break; 2910 case UNION: 2911 /* union %s never defined */ 2912 warning(234, sym->s_name); 2913 break; 2914 case ENUM: 2915 /* enum %s never defined */ 2916 warning(235, sym->s_name); 2917 break; 2918 default: 2919 LERROR("chktusg()"); 2920 } 2921} 2922 2923/* 2924 * Called after the entire translation unit has been parsed. 2925 * Changes tentative definitions in definitions. 2926 * Performs some tests on global Symbols. Detected Problems are: 2927 * - defined variables of incomplete type 2928 * - constant variables which are not initialized 2929 * - static symbols which are never used 2930 */ 2931void 2932chkglsyms(void) 2933{ 2934 sym_t *sym; 2935 pos_t cpos; 2936 2937 if (blklev != 0 || dcs->d_nxt != NULL) 2938 norecover(); 2939 2940 STRUCT_ASSIGN(cpos, curr_pos); 2941 2942 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) { 2943 if (sym->s_blklev == -1) 2944 continue; 2945 if (sym->s_kind == FVFT) { 2946 chkglvar(sym); 2947 } else if (sym->s_kind == FTAG) { 2948 chktusg(sym); 2949 } else { 2950 if (sym->s_kind != FMOS) 2951 LERROR("chkglsyms()"); 2952 } 2953 } 2954 2955 STRUCT_ASSIGN(curr_pos, cpos); 2956} 2957 2958static void 2959chkglvar(sym_t *sym) 2960{ 2961 2962 if (sym->s_scl == TYPEDEF || sym->s_scl == ENUMCON) 2963 return; 2964 2965 if (sym->s_scl != EXTERN && sym->s_scl != STATIC) 2966 LERROR("chkglvar()"); 2967 2968 glchksz(sym); 2969 2970 if (sym->s_scl == STATIC) { 2971 if (sym->s_type->t_tspec == FUNC) { 2972 if (sym->s_used && sym->s_def != DEF) { 2973 STRUCT_ASSIGN(curr_pos, sym->s_upos); 2974 /* static func. called but not def.. */ 2975 error(225, sym->s_name); 2976 } 2977 } 2978 if (!sym->s_used) { 2979 STRUCT_ASSIGN(curr_pos, sym->s_dpos); 2980 if (sym->s_type->t_tspec == FUNC) { 2981 if (sym->s_def == DEF) { 2982 if (!sym->s_inline) 2983 /* static function %s unused */ 2984 warning(236, sym->s_name); 2985 } else { 2986 /* static function %s decl. but ... */ 2987 warning(290, sym->s_name); 2988 } 2989 } else if (!sym->s_set) { 2990 /* static variable %s unused */ 2991 warning(226, sym->s_name); 2992 } else { 2993 /* static variable %s set but not used */ 2994 warning(307, sym->s_name); 2995 } 2996 } 2997 if (!tflag && sym->s_def == TDEF && sym->s_type->t_const) { 2998 STRUCT_ASSIGN(curr_pos, sym->s_dpos); 2999 /* const object %s should have initializer */ 3000 warning(227, sym->s_name); 3001 } 3002 } 3003} 3004 3005static void 3006glchksz(sym_t *sym) 3007{ 3008 3009 if (sym->s_def == TDEF) { 3010 if (sym->s_type->t_tspec == FUNC) 3011 /* 3012 * this can happen if a syntax error occurred 3013 * after a function declaration 3014 */ 3015 return; 3016 STRUCT_ASSIGN(curr_pos, sym->s_dpos); 3017 if (length(sym->s_type, sym->s_name) == 0 && 3018 sym->s_type->t_tspec == ARRAY && sym->s_type->t_dim == 0) { 3019 /* empty array declaration: %s */ 3020 if (tflag || (sym->s_scl == EXTERN && !sflag)) { 3021 warning(190, sym->s_name); 3022 } else { 3023 error(190, sym->s_name); 3024 } 3025 } 3026 } 3027} 3028 3029/* 3030 * Prints information about location of previous definition/declaration. 3031 */ 3032void 3033prevdecl(int msg, sym_t *psym) 3034{ 3035 pos_t cpos; 3036 3037 if (!rflag) 3038 return; 3039 3040 STRUCT_ASSIGN(cpos, curr_pos); 3041 STRUCT_ASSIGN(curr_pos, psym->s_dpos); 3042 if (msg != -1) { 3043 message(msg, psym->s_name); 3044 } else if (psym->s_def == DEF || psym->s_def == TDEF) { 3045 /* previous definition of %s */ 3046 message(261, psym->s_name); 3047 } else { 3048 /* previous declaration of %s */ 3049 message(260, psym->s_name); 3050 } 3051 STRUCT_ASSIGN(curr_pos, cpos); 3052} 3053