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