Initial import from FreeBSD RELENG_4:
[games.git] / usr.bin / xlint / lint1 / lint1.h
1 /*      $NetBSD: lint1.h,v 1.6 1995/10/02 17:31:41 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         INLINE          /* only used by the parser */
183 } scl_t;
184
185 /*
186  * symbol table entry
187  */
188 typedef struct sym {
189         const   char *s_name;   /* name */
190         pos_t   s_dpos;         /* position of last (prototype)definition,
191                                    prototypedeclaration, no-prototype-def.,
192                                    tentative definition or declaration,
193                                    in this order */
194         pos_t   s_spos;         /* position of first initialisation */
195         pos_t   s_upos;         /* position of first use */
196         symt_t  s_kind;         /* type of symbol */
197         u_int   s_keyw : 1;     /* keyword */
198         u_int   s_field : 1;    /* bit-field */
199         u_int   s_set : 1;      /* variable set, label defined */
200         u_int   s_used : 1;     /* variable/label used */
201         u_int   s_arg : 1;      /* symbol is function argument */
202         u_int   s_reg : 1;      /* symbol is register variable */
203         u_int   s_defarg : 1;   /* undefined symbol in old style function
204                                    definition */
205         u_int   s_rimpl : 1;    /* return value of function implizit decl. */
206         u_int   s_osdef : 1;    /* symbol stems from old style function def. */
207         u_int   s_inline : 1;   /* true if this is a inline function */
208         struct  sym *s_xsym;    /* for local declared external symbols pointer
209                                    to external symbol with same name */
210         def_t   s_def;          /* declared, tentative defined, defined */
211         scl_t   s_scl;          /* storage class */
212         int     s_blklev;       /* level of declaration, -1 if not in symbol
213                                    table */
214         type_t  *s_type;        /* type */
215         val_t   s_value;        /* value (if enumcon) */
216         union {
217                 str_t   *_s_st; /* tag, if it is a struct/union member */
218                 enum_t  *_s_et; /* tag, if it is a enumerator */
219                 tspec_t _s_tsp; /* type (only for keywords) */
220                 tqual_t _s_tqu; /* qualifier (only for keywords) */
221                 struct  sym *_s_args; /* arguments in old style function
222                                          definitions */
223         } u;
224         struct  sym *s_link;    /* next symbol with same hash value */
225         struct  sym **s_rlink;  /* pointer to s_link of prev. symbol */
226         struct  sym *s_nxt;     /* next struct/union member, enumerator,
227                                    argument */
228         struct  sym *s_dlnxt;   /* next symbol declared on same level */
229 } sym_t;
230
231 #define s_styp  u._s_st
232 #define s_etyp  u._s_et
233 #define s_tspec u._s_tsp
234 #define s_tqual u._s_tqu
235 #define s_args  u._s_args
236
237 /*
238  * Used to keep some informations about symbols before they are entered
239  * into the symbol table.
240  */
241 typedef struct sbuf {
242         const   char *sb_name;          /* name of symbol */
243         size_t  sb_len;                 /* length (without '\0') */
244         int     sb_hash;                /* hash value */
245         sym_t   *sb_sym;                /* symbol table entry */
246         struct  sbuf *sb_nxt;           /* for freelist */
247 } sbuf_t;
248
249
250 /*
251  * tree node
252  */
253 typedef struct tnode {
254         op_t    tn_op;          /* operator */
255         type_t  *tn_type;       /* type */
256         u_int   tn_lvalue : 1;  /* node is lvalue */
257         u_int   tn_cast : 1;    /* if tn_op == CVT its an explizit cast */
258         u_int   tn_parn : 1;    /* node parenthesized */
259         union {
260                 struct {
261                         struct  tnode *_tn_left;        /* (left) operand */
262                         struct  tnode *_tn_right;       /* right operand */
263                 } tn_s;
264                 sym_t   *_tn_sym;       /* symbol if op == NAME */
265                 val_t   *_tn_val;       /* value if op == CON */
266                 strg_t  *_tn_strg;      /* string if op == STRING */
267         } tn_u;
268 } tnode_t;
269
270 #define tn_left tn_u.tn_s._tn_left
271 #define tn_right tn_u.tn_s._tn_right
272 #define tn_sym  tn_u._tn_sym
273 #define tn_val  tn_u._tn_val
274 #define tn_strg tn_u._tn_strg
275
276 /*
277  * For nested declarations a stack exists, which holds all information
278  * needed for the current level. dcs points to the top element of this
279  * stack.
280  *
281  * ctx describes the context of the current declaration. Its value is
282  * one of
283  *      EXTERN  global declarations
284  *      MOS oder MOU declarations of struct or union members
285  *      ENUMCON declarations of enums
286  *      ARG     declaration of arguments in old style function definitions
287  *      PARG    declaration of arguments in function prototypes
288  *      AUTO    declaration of local symbols
289  *      ABSTRACT abstract declarations (sizeof, casts)
290  *
291  */
292 typedef struct dinfo {
293         tspec_t d_atyp;         /* VOID, CHAR, INT, FLOAT or DOUBLE */
294         tspec_t d_smod;         /* SIGNED or UNSIGN */
295         tspec_t d_lmod;         /* SHORT, LONG or QUAD */
296         scl_t   d_scl;          /* storage class */
297         type_t  *d_type;        /* after deftyp() pointer to the type used
298                                    for all declarators */
299         sym_t   *d_rdcsym;      /* redeclared symbol */
300         int     d_offset;       /* offset of next structure member */
301         int     d_stralign;     /* alignment required for current structure */
302         scl_t   d_ctx;          /* context of declaration */
303         u_int   d_const : 1;    /* const in declaration specifiers */
304         u_int   d_volatile : 1; /* volatile in declaration specifiers */
305         u_int   d_inline : 1;   /* inline in declaration specifiers */
306         u_int   d_mscl : 1;     /* multiple storage classes */
307         u_int   d_terr : 1;     /* invalid type combination */
308         u_int   d_nedecl : 1;   /* 1 if at least a tag is declared */
309         u_int   d_vararg : 1;   /* ... in in current function decl. */
310         u_int   d_proto : 1;    /* current funct. decl. is prototype */
311         u_int   d_notyp : 1;    /* set if no type specifier was present */
312         u_int   d_asm : 1;      /* set if d_ctx == AUTO and asm() present */
313         type_t  *d_tagtyp;      /* tag during member declaration */
314         sym_t   *d_fargs;       /* list of arguments during function def. */
315         pos_t   d_fdpos;        /* position of function definition */
316         sym_t   *d_dlsyms;      /* first symbol declared at this level */
317         sym_t   **d_ldlsym;     /* points to s_dlnxt in last symbol decl.
318                                    at this level */
319         sym_t   *d_fpsyms;      /* symbols defined in prototype */
320         struct  dinfo *d_nxt;   /* next level */
321 } dinfo_t;
322
323 /*
324  * Type of stack which is used for initialisation of aggregate types.
325  */
326 typedef struct  istk {
327         type_t  *i_type;                /* type of initialisation */
328         type_t  *i_subt;                /* type of next level */
329         u_int   i_brace : 1;            /* need } for pop */
330         u_int   i_nolimit : 1;          /* incomplete array type */
331         sym_t   *i_mem;                 /* next structure member */
332         int     i_cnt;                  /* # of remaining elements */
333         struct  istk *i_nxt;            /* previous level */
334 } istk_t;
335
336 /*
337  * Used to collect information about pointers and qualifiers in
338  * declarators.
339  */
340 typedef struct pqinf {
341         int     p_pcnt;                 /* number of asterisks */
342         u_int   p_const : 1;
343         u_int   p_volatile : 1;
344         struct  pqinf *p_nxt;
345 } pqinf_t;
346
347 /*
348  * Case values are stored in a list of type clst_t.
349  */
350 typedef struct clst {
351         val_t   cl_val;
352         struct  clst *cl_nxt;
353 } clst_t;
354
355 /*
356  * Used to keep informations about nested control statements.
357  */
358 typedef struct cstk {
359         int     c_env;                  /* type of statement (T_IF, ...) */
360         u_int   c_loop : 1;             /* continue && break are valid */
361         u_int   c_switch : 1;           /* case && break are valid */
362         u_int   c_break : 1;            /* loop/switch has break */
363         u_int   c_cont : 1;             /* loop has continue */
364         u_int   c_default : 1;          /* switch has default */
365         u_int   c_infinite : 1;         /* break condition always false
366                                            (for (;;), while (1)) */
367         u_int   c_rchif : 1;            /* end of if-branch reached */
368         u_int   c_noretval : 1;         /* had "return;" */
369         u_int   c_retval : 1;           /* had "return (e);" */
370         type_t  *c_swtype;              /* type of switch expression */
371         clst_t  *c_clst;                /* list of case values */
372         struct  mbl *c_fexprm;          /* saved memory for end of loop
373                                            expression in for() */
374         tnode_t *c_f3expr;              /* end of loop expr in for() */
375         pos_t   c_fpos;                 /* position of end of loop expr */
376         pos_t   c_cfpos;                /* same for csrc_pos */
377         struct  cstk *c_nxt;            /* outer control statement */
378 } cstk_t;
379
380 #include "externs1.h"