Home | History | Annotate | Line # | Download | only in lint1
lint1.h revision 1.3
      1 /*	$NetBSD: lint1.h,v 1.3 1995/10/02 17:08:40 jpo Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995 Jochen Pohl
      5  * All Rights Reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Jochen Pohl for
     18  *	The NetBSD Project.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include "lint.h"
     35 #include "op.h"
     36 
     37 /*
     38  * Describes the position of a declaration or anything else.
     39  */
     40 typedef struct {
     41 	int	p_line;
     42 	const	char *p_file;
     43 } pos_t;
     44 
     45 /*
     46  * Strings cannot be referenced to simply by a pointer to its first
     47  * char. This is because strings can contain NUL characters other than the
     48  * trailing NUL.
     49  *
     50  * Strings are stored with a trailing NUL.
     51  */
     52 typedef	struct strg {
     53 	tspec_t	st_tspec;		/* CHAR or WCHAR */
     54 	size_t	st_len;			/* length without trailing NUL */
     55 	union {
     56 		u_char	*_st_cp;
     57 		wchar_t	*_st_wcp;
     58 	} st_u;
     59 } strg_t;
     60 
     61 #define st_cp	st_u._st_cp
     62 #define	st_wcp	st_u._st_wcp
     63 
     64 /*
     65  * qualifiers (only for lex/yacc interface)
     66  */
     67 typedef enum {
     68 	CONST, VOLATILE
     69 } tqual_t;
     70 
     71 /*
     72  * Integer and floating point values are stored in this structure
     73  */
     74 typedef struct {
     75 	tspec_t	v_tspec;
     76 	int	v_ansiu;		/* set if an integer constant is
     77 					   unsigned in ANSI C */
     78 	union {
     79 		quad_t	_v_quad;	/* integers */
     80 		ldbl_t	_v_ldbl;	/* floats */
     81 	} v_u;
     82 } val_t;
     83 
     84 #define v_quad	v_u._v_quad
     85 #define v_ldbl	v_u._v_ldbl
     86 
     87 /*
     88  * Structures of type str_t uniqely identify structures. This can't
     89  * be done in structures of type type_t, because these are copied
     90  * if they must be modified. So it would not be possible to check
     91  * if to structures are identical by comparing the pointers to
     92  * the type structures.
     93  *
     94  * The typename is used if the structure is unnamed to identify
     95  * the structure type in pass 2.
     96  */
     97 typedef	struct {
     98 	u_int	size;		/* size in bit */
     99 	u_int	align : 15;	/* alignment in bit */
    100 	u_int	sincompl : 1;	/* set if incomplete type */
    101 	struct	sym *memb;	/* list of members */
    102 	struct	sym *stag;	/* symbol table entry of tag */
    103 	struct	sym *stdef;	/* symbol table entry of first typename */
    104 } str_t;
    105 
    106 /*
    107  * same as above for enums
    108  */
    109 typedef	struct {
    110 	u_int	eincompl : 1;	/* incomplete enum type */
    111 	struct	sym *elem;	/* list of enumerators */
    112 	struct	sym *etag;	/* symbol table entry of tag */
    113 	struct	sym *etdef;	/* symbol table entry of first typename */
    114 } enum_t;
    115 
    116 /*
    117  * Types are represented by concatenation of structures of type type_t
    118  * via t_subt.
    119  */
    120 typedef	struct type {
    121 	tspec_t	t_tspec;	/* type specifier */
    122 	u_int	t_aincompl : 1;	/* incomplete array type */
    123 	u_int	t_const : 1;	/* const modifier */
    124 	u_int	t_volatile : 1;	/* volatile modifier */
    125 	u_int	t_proto : 1;	/* function prototype (t_args valid) */
    126 	u_int	t_vararg : 1;	/* protoype with ... */
    127 	u_int	t_typedef : 1;	/* type defined with typedef */
    128 	u_int	t_isfield : 1;	/* type is bitfield */
    129 	u_int	t_isenum : 1;	/* type is (or was) enum (t_enum valid) */
    130 	union {
    131 		int	_t_dim;		/* dimension */
    132 		str_t	*_t_str;	/* struct/union tag */
    133 		enum_t	*_t_enum;	/* enum tag */
    134 		struct	sym *_t_args;	/* arguments (if t_proto) */
    135 		struct {
    136 			u_int	_t_flen : 8;	/* length of bit-field */
    137 			u_int	_t_foffs : 24;	/* offset of bit-field */
    138 		} _t_u;
    139 	} t_u;
    140 	struct	type *t_subt;	/* element type (arrays), return value
    141 				   (functions), or type pointer points to */
    142 } type_t;
    143 
    144 #define	t_dim	t_u._t_dim
    145 #define	t_str	t_u._t_str
    146 #define	t_field	t_u._t_field
    147 #define	t_enum	t_u._t_enum
    148 #define	t_args	t_u._t_args
    149 #define	t_flen	t_u._t_u._t_flen
    150 #define	t_foffs	t_u._t_u._t_foffs
    151 
    152 /*
    153  * types of symbols
    154  */
    155 typedef	enum {
    156 	FVFT,		/* variables, functions, type names, enums */
    157 	FMOS,		/* members of structs or unions */
    158 	FTAG,		/* tags */
    159 	FLAB		/* labels */
    160 } symt_t;
    161 
    162 /*
    163  * storage classes
    164  */
    165 typedef enum {
    166 	NOSCL,
    167 	EXTERN,		/* external symbols (indep. of decl_t) */
    168 	STATIC,		/* static symbols (local and global) */
    169 	AUTO,		/* automatic symbols (except register) */
    170 	REG,		/* register */
    171 	TYPEDEF,	/* typedef */
    172 	STRTAG,
    173 	UNIONTAG,
    174 	ENUMTAG,
    175 	MOS,		/* member of struct */
    176 	MOU,		/* member of union */
    177 	ENUMCON,	/* enumerator */
    178 	ABSTRACT,	/* abstract symbol (sizeof, casts, unnamed argument) */
    179 	ARG,		/* argument */
    180 	PARG		/* used in declaration stack during prototype
    181 			   declaration */
    182 } scl_t;
    183 
    184 /*
    185  * symbol table entry
    186  */
    187 typedef	struct sym {
    188 	const	char *s_name;	/* name */
    189 	pos_t	s_dpos;		/* position of last (prototype)definition,
    190 				   prototypedeclaration, no-prototype-def.,
    191 				   tentative definition or declaration,
    192 				   in this order */
    193 	pos_t	s_spos;		/* position of first initialisation */
    194 	pos_t	s_upos;		/* position of first use */
    195 	symt_t	s_kind;		/* type of symbol */
    196 	u_int	s_keyw : 1;	/* keyword */
    197 	u_int	s_field : 1;	/* bit-field */
    198 	u_int	s_set : 1;	/* variable set, label defined */
    199 	u_int	s_used : 1;	/* variable/label used */
    200 	u_int	s_arg : 1;	/* symbol is function argument */
    201 	u_int	s_reg : 1;	/* symbol is register variable */
    202 	u_int	s_defarg : 1;	/* undefined symbol in old style function
    203 				   definition */
    204 	u_int	s_rimpl : 1;	/* return value of function implizit decl. */
    205 	u_int	s_osdef : 1;	/* symbol stems from old style function def. */
    206 	struct	sym *s_xsym;	/* for local declared external symbols pointer
    207 				   to external symbol with same name */
    208 	def_t	s_def;		/* declared, tentative defined, defined */
    209 	scl_t	s_scl;		/* storage class */
    210 	int	s_blklev;	/* level of declaration, -1 if not in symbol
    211 				   table */
    212 	type_t	*s_type;	/* type */
    213 	val_t	s_value;	/* value (if enumcon) */
    214 	union {
    215 		str_t	*_s_st;	/* tag, if it is a struct/union member */
    216 		enum_t	*_s_et;	/* tag, if it is a enumerator */
    217 		tspec_t	_s_tsp;	/* type (only for keywords) */
    218 		tqual_t	_s_tqu;	/* qualifier (only for keywords) */
    219 		struct	sym *_s_args; /* arguments in old style function
    220 					 definitions */
    221 	} u;
    222 	struct	sym *s_link;	/* next symbol with same hash value */
    223 	struct	sym **s_rlink;	/* pointer to s_link of prev. symbol */
    224 	struct	sym *s_nxt;	/* next struct/union member, enumerator,
    225 				   argument */
    226 	struct	sym *s_dlnxt; 	/* next symbol declared on same level */
    227 } sym_t;
    228 
    229 #define	s_styp	u._s_st
    230 #define	s_etyp	u._s_et
    231 #define	s_tspec	u._s_tsp
    232 #define	s_tqual	u._s_tqu
    233 #define	s_args	u._s_args
    234 
    235 /*
    236  * Used to keep some informations about symbols before they are entered
    237  * into the symbol table.
    238  */
    239 typedef	struct sbuf {
    240 	const	char *sb_name;		/* name of symbol */
    241 	size_t	sb_len;			/* length (without '\0') */
    242 	int	sb_hash;		/* hash value */
    243 	sym_t	*sb_sym;		/* symbol table entry */
    244 	struct	sbuf *sb_nxt;		/* for freelist */
    245 } sbuf_t;
    246 
    247 
    248 /*
    249  * tree node
    250  */
    251 typedef	struct tnode {
    252 	op_t	tn_op;		/* operator */
    253 	type_t	*tn_type;	/* type */
    254 	u_int	tn_lvalue : 1;	/* node is lvalue */
    255 	u_int	tn_cast : 1;	/* if tn_op == CVT its an explizit cast */
    256 	u_int	tn_parn : 1;	/* node parenthesized */
    257 	union {
    258 		struct {
    259 			struct	tnode *_tn_left;	/* (left) operand */
    260 			struct	tnode *_tn_right;	/* right operand */
    261 		} tn_s;
    262 		sym_t	*_tn_sym;	/* symbol if op == NAME */
    263 		val_t	*_tn_val;	/* value if op == CON */
    264 		strg_t	*_tn_strg;	/* string if op == STRING */
    265 	} tn_u;
    266 } tnode_t;
    267 
    268 #define	tn_left	tn_u.tn_s._tn_left
    269 #define tn_right tn_u.tn_s._tn_right
    270 #define tn_sym	tn_u._tn_sym
    271 #define	tn_val	tn_u._tn_val
    272 #define	tn_strg	tn_u._tn_strg
    273 
    274 /*
    275  * For nested declarations a stack exists, which holds all information
    276  * needed for the current level. dcs points to the top element of this
    277  * stack.
    278  *
    279  * ctx describes the context of the current declaration. Its value is
    280  * one of
    281  *	EXTERN	global declarations
    282  *	MOS oder MOU declarations of struct or union members
    283  *	ENUMCON	declarations of enums
    284  *	ARG	declaration of arguments in old style function definitions
    285  *	PARG	declaration of arguments in function prototypes
    286  *	AUTO	declaration of local symbols
    287  *	ABSTRACT abstract declarations (sizeof, casts)
    288  *
    289  */
    290 typedef	struct dinfo {
    291 	tspec_t	d_atyp;		/* VOID, CHAR, INT, FLOAT or DOUBLE */
    292 	tspec_t	d_smod;		/* SIGNED or UNSIGN */
    293 	tspec_t	d_lmod;		/* SHORT, LONG or QUAD */
    294 	scl_t	d_scl;		/* storage class */
    295 	type_t	*d_type;	/* after deftyp() pointer to the type used
    296 				   for all declarators */
    297 	sym_t	*d_rdcsym;	/* redeclared symbol */
    298 	int	d_offset;	/* offset of next structure member */
    299 	int	d_stralign;	/* alignment required for current structure */
    300 	scl_t	d_ctx;		/* context of declaration */
    301 	u_int	d_const : 1;	/* const in declaration specifiers */
    302 	u_int	d_volatile : 1;	/* volatile in declaration specifiers */
    303 	u_int	d_mscl : 1;	/* multiple storage classes */
    304 	u_int	d_terr : 1;	/* invalid type combination */
    305 	u_int	d_nedecl : 1;	/* 1 if at least a tag is declared */
    306 	u_int	d_vararg : 1;	/* ... in in current function decl. */
    307 	u_int	d_proto : 1;	/* current funct. decl. is prototype */
    308 	u_int	d_notyp : 1;	/* set if no type specifier was present */
    309 	type_t	*d_tagtyp;	/* tag during member declaration */
    310 	sym_t	*d_fargs;	/* list of arguments during function def. */
    311 	type_t	*d_ftype;	/* type as specified in function def. */
    312 	pos_t	d_fdpos;	/* position of function definition */
    313 	sym_t	*d_dlsyms;	/* first symbol declared at this level */
    314 	sym_t	**d_ldlsym;	/* points to s_dlnxt in last symbol decl.
    315 				   at this level */
    316 	sym_t	*d_fpsyms;	/* symbols defined in prototype */
    317 	struct	dinfo *d_nxt;	/* next level */
    318 } dinfo_t;
    319 
    320 /*
    321  * Type of stack which is used for initialisation of aggregate types.
    322  */
    323 typedef	struct	istk {
    324 	type_t	*i_type;		/* type of initialisation */
    325 	type_t	*i_subt;		/* type of next level */
    326 	u_int	i_brace : 1;		/* need } for pop */
    327 	u_int	i_nolimit : 1;		/* incomplete array type */
    328 	sym_t	*i_mem;			/* next structure member */
    329 	int	i_cnt;			/* # of remaining elements */
    330 	struct	istk *i_nxt;		/* previous level */
    331 } istk_t;
    332 
    333 /*
    334  * Used to collect information about pointers and qualifiers in
    335  * declarators.
    336  */
    337 typedef	struct pqinf {
    338 	int	p_pcnt;			/* number of asterisks */
    339 	u_int	p_const : 1;
    340 	u_int	p_volatile : 1;
    341 	struct	pqinf *p_nxt;
    342 } pqinf_t;
    343 
    344 /*
    345  * Case values are stored in a list of type clst_t.
    346  */
    347 typedef	struct clst {
    348 	val_t	cl_val;
    349 	struct	clst *cl_nxt;
    350 } clst_t;
    351 
    352 /*
    353  * Used to keep informations about nested control statements.
    354  */
    355 typedef struct cstk {
    356 	int	c_env;			/* type of statement (T_IF, ...) */
    357 	u_int	c_loop : 1;		/* continue && break are valid */
    358 	u_int	c_switch : 1;		/* case && break are valid */
    359 	u_int	c_break : 1;		/* loop/switch has break */
    360 	u_int	c_cont : 1;		/* loop has continue */
    361 	u_int	c_default : 1;		/* switch has default */
    362 	u_int	c_infinite : 1;		/* break condition always false
    363 					   (for (;;), while (1)) */
    364 	u_int	c_rchif : 1;		/* end of if-branch reached */
    365 	u_int	c_noretval : 1;		/* had "return;" */
    366 	u_int	c_retval : 1;		/* had "return (e);" */
    367 	type_t	*c_swtype;		/* type of switch expression */
    368 	clst_t	*c_clst;		/* list of case values */
    369 	struct	mbl *c_fexprm;		/* saved memory for end of loop
    370 					   expression in for() */
    371 	tnode_t	*c_f3expr;		/* end of loop expr in for() */
    372 	pos_t	c_fpos;			/* position of end of loop expr */
    373 	pos_t	c_cfpos;	        /* same for csrc_pos */
    374 	struct	cstk *c_nxt;		/* outer control statement */
    375 } cstk_t;
    376 
    377 #include "externs1.h"
    378