Home | History | Annotate | Line # | Download | only in lint1
lint1.h revision 1.148
      1  1.148    rillig /* $NetBSD: lint1.h,v 1.148 2022/04/09 16:02:14 rillig Exp $ */
      2    1.2       cgd 
      3    1.1       cgd /*
      4    1.7       cgd  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
      5    1.1       cgd  * Copyright (c) 1994, 1995 Jochen Pohl
      6    1.1       cgd  * All Rights Reserved.
      7    1.1       cgd  *
      8    1.1       cgd  * Redistribution and use in source and binary forms, with or without
      9    1.1       cgd  * modification, are permitted provided that the following conditions
     10    1.1       cgd  * are met:
     11    1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     12    1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     13    1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14    1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     15    1.1       cgd  *    documentation and/or other materials provided with the distribution.
     16    1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     17    1.1       cgd  *    must display the following acknowledgement:
     18    1.1       cgd  *      This product includes software developed by Jochen Pohl for
     19    1.1       cgd  *	The NetBSD Project.
     20    1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     21    1.1       cgd  *    derived from this software without specific prior written permission.
     22    1.1       cgd  *
     23    1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24    1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25    1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26    1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27    1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28    1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29    1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30    1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31    1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32    1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33    1.1       cgd  */
     34    1.1       cgd 
     35    1.1       cgd #include "lint.h"
     36   1.49    rillig #include "err-msgs.h"
     37    1.1       cgd #include "op.h"
     38   1.12        tv 
     39   1.53    rillig #define LWARN_BAD	(-3)
     40   1.53    rillig #define LWARN_ALL	(-2)
     41   1.53    rillig #define LWARN_NONE	(-1)
     42   1.27  christos 
     43    1.1       cgd /*
     44    1.1       cgd  * Describes the position of a declaration or anything else.
     45   1.96    rillig  *
     46   1.96    rillig  * FIXME: Just a single file:lineno pair is not enough to accurately describe
     47   1.96    rillig  *  the position of a symbol.  The whole inclusion path at that point must be
     48   1.96    rillig  *  stored as well.  This makes a difference for symbols from included
     49   1.96    rillig  *  headers, see print_stack_trace.
     50    1.1       cgd  */
     51    1.1       cgd typedef struct {
     52   1.92    rillig 	const	char *p_file;
     53    1.1       cgd 	int	p_line;
     54    1.7       cgd 	int	p_uniq;			/* uniquifier */
     55    1.1       cgd } pos_t;
     56    1.7       cgd 
     57    1.7       cgd /* Copies curr_pos, keeping things unique. */
     58   1.34    rillig #define	UNIQUE_CURR_POS(pos)						\
     59   1.34    rillig 	do {								\
     60   1.45    rillig 		(pos) = curr_pos;					\
     61   1.34    rillig 		curr_pos.p_uniq++;					\
     62   1.34    rillig 		if (curr_pos.p_file == csrc_pos.p_file)			\
     63   1.34    rillig 			csrc_pos.p_uniq++;				\
     64  1.100    rillig 	} while (false)
     65    1.1       cgd 
     66    1.1       cgd /*
     67  1.125    rillig  * Strings cannot be referenced simply by a pointer to their first
     68    1.1       cgd  * char. This is because strings can contain NUL characters other than the
     69    1.1       cgd  * trailing NUL.
     70    1.1       cgd  *
     71    1.1       cgd  * Strings are stored with a trailing NUL.
     72    1.1       cgd  */
     73    1.1       cgd typedef	struct strg {
     74  1.142    rillig 	bool	st_char;	/* string doesn't have an 'L' prefix */
     75  1.142    rillig 	size_t	st_len;		/* length without trailing NUL */
     76  1.142    rillig 	void	*st_mem;	/* char[] for st_char, or wchar_t[] */
     77    1.1       cgd } strg_t;
     78    1.1       cgd 
     79    1.1       cgd /*
     80    1.1       cgd  * qualifiers (only for lex/yacc interface)
     81    1.1       cgd  */
     82    1.1       cgd typedef enum {
     83   1.31  christos 	CONST, VOLATILE, RESTRICT, THREAD
     84    1.1       cgd } tqual_t;
     85    1.1       cgd 
     86  1.104    rillig /* An integer or floating-point value. */
     87    1.1       cgd typedef struct {
     88    1.1       cgd 	tspec_t	v_tspec;
     89  1.105    rillig 	/*
     90  1.105    rillig 	 * Set if an integer constant is unsigned only in C90 and later, but
     91  1.105    rillig 	 * not in traditional C.
     92  1.105    rillig 	 *
     93  1.105    rillig 	 * See the operators table in ops.def, columns "l r".
     94  1.105    rillig 	 */
     95  1.105    rillig 	bool	v_unsigned_since_c90;
     96    1.1       cgd 	union {
     97   1.13   thorpej 		int64_t	_v_quad;	/* integers */
     98    1.1       cgd 		ldbl_t	_v_ldbl;	/* floats */
     99    1.1       cgd 	} v_u;
    100    1.1       cgd } val_t;
    101    1.1       cgd 
    102    1.1       cgd #define v_quad	v_u._v_quad
    103    1.1       cgd #define v_ldbl	v_u._v_ldbl
    104    1.1       cgd 
    105    1.1       cgd /*
    106   1.62    rillig  * Structures of type struct_or_union uniquely identify structures. This can't
    107    1.1       cgd  * be done in structures of type type_t, because these are copied
    108    1.1       cgd  * if they must be modified. So it would not be possible to check
    109   1.29  dholland  * if two structures are identical by comparing the pointers to
    110    1.1       cgd  * the type structures.
    111    1.1       cgd  *
    112    1.1       cgd  * The typename is used if the structure is unnamed to identify
    113    1.1       cgd  * the structure type in pass 2.
    114    1.1       cgd  */
    115    1.1       cgd typedef	struct {
    116  1.126    rillig 	unsigned int sou_size_in_bits;
    117  1.126    rillig 	unsigned short sou_align_in_bits;
    118  1.132    rillig 	bool	sou_incomplete:1;
    119   1.62    rillig 	struct	sym *sou_first_member;
    120   1.62    rillig 	struct	sym *sou_tag;
    121   1.62    rillig 	struct	sym *sou_first_typedef;
    122   1.62    rillig } struct_or_union;
    123    1.1       cgd 
    124    1.1       cgd /*
    125    1.1       cgd  * same as above for enums
    126    1.1       cgd  */
    127    1.1       cgd typedef	struct {
    128  1.132    rillig 	bool	en_incomplete:1;
    129   1.63    rillig 	struct	sym *en_first_enumerator;
    130   1.63    rillig 	struct	sym *en_tag;
    131   1.63    rillig 	struct	sym *en_first_typedef;
    132   1.63    rillig } enumeration;
    133    1.1       cgd 
    134    1.1       cgd /*
    135   1.93    rillig  * The type of an expression or object. Complex types are formed via t_subt
    136   1.93    rillig  * (for arrays, pointers and functions), as well as t_str.
    137    1.1       cgd  */
    138   1.94    rillig struct lint1_type {
    139    1.1       cgd 	tspec_t	t_tspec;	/* type specifier */
    140  1.132    rillig 	bool	t_incomplete_array:1;
    141  1.132    rillig 	bool	t_const:1;	/* const modifier */
    142  1.132    rillig 	bool	t_volatile:1;	/* volatile modifier */
    143  1.132    rillig 	bool	t_proto:1;	/* function prototype (t_args valid) */
    144  1.132    rillig 	bool	t_vararg:1;	/* prototype with '...' */
    145  1.132    rillig 	bool	t_typedef:1;	/* type defined with typedef */
    146  1.132    rillig 	bool	t_bitfield:1;
    147  1.109    rillig 	/*
    148  1.109    rillig 	 * Either the type is currently an enum (having t_tspec ENUM), or
    149  1.109    rillig 	 * it is an integer type (typically INT) that has been implicitly
    150  1.109    rillig 	 * converted from an enum type.  In both cases, t_enum is valid.
    151  1.109    rillig 	 *
    152  1.109    rillig 	 * The information about a former enum type is retained to allow
    153  1.109    rillig 	 * type checks in expressions such as ((var1 & 0x0001) == var2), to
    154  1.109    rillig 	 * detect when var1 and var2 are from incompatible enum types.
    155  1.109    rillig 	 */
    156  1.132    rillig 	bool	t_is_enum:1;
    157  1.132    rillig 	bool	t_packed:1;
    158    1.1       cgd 	union {
    159   1.74    rillig 		int	_t_dim;		/* dimension (if ARRAY) */
    160   1.63    rillig 		struct_or_union	*_t_str;
    161   1.63    rillig 		enumeration	*_t_enum;
    162    1.1       cgd 		struct	sym *_t_args;	/* arguments (if t_proto) */
    163    1.1       cgd 	} t_u;
    164    1.8        pk 	struct {
    165  1.132    rillig 		unsigned int	_t_flen:8;	/* length of bit-field */
    166  1.132    rillig 		unsigned int	_t_foffs:24;	/* offset of bit-field */
    167    1.8        pk 	} t_b;
    168   1.94    rillig 	struct	lint1_type *t_subt; /* element type (if ARRAY),
    169   1.94    rillig 				 * return value (if FUNC),
    170   1.94    rillig 				 * target type (if PTR) */
    171   1.17  christos };
    172    1.1       cgd 
    173    1.1       cgd #define	t_dim	t_u._t_dim
    174  1.119    rillig /* TODO: rename t_str to t_sou, to avoid confusing it with strings. */
    175    1.1       cgd #define	t_str	t_u._t_str
    176    1.1       cgd #define	t_enum	t_u._t_enum
    177    1.1       cgd #define	t_args	t_u._t_args
    178    1.8        pk #define	t_flen	t_b._t_flen
    179    1.8        pk #define	t_foffs	t_b._t_foffs
    180    1.1       cgd 
    181    1.1       cgd /*
    182    1.1       cgd  * types of symbols
    183    1.1       cgd  */
    184    1.1       cgd typedef	enum {
    185    1.1       cgd 	FVFT,		/* variables, functions, type names, enums */
    186   1.39    rillig 	FMEMBER,	/* members of structs or unions */
    187    1.1       cgd 	FTAG,		/* tags */
    188   1.39    rillig 	FLABEL		/* labels */
    189    1.1       cgd } symt_t;
    190    1.1       cgd 
    191    1.1       cgd /*
    192  1.115    rillig  * storage classes and related things
    193    1.1       cgd  */
    194    1.1       cgd typedef enum {
    195    1.1       cgd 	NOSCL,
    196  1.115    rillig 	EXTERN,		/* external symbols (independent of decl_t) */
    197    1.1       cgd 	STATIC,		/* static symbols (local and global) */
    198    1.1       cgd 	AUTO,		/* automatic symbols (except register) */
    199    1.1       cgd 	REG,		/* register */
    200    1.1       cgd 	TYPEDEF,	/* typedef */
    201   1.65    rillig 	STRUCT_TAG,
    202   1.65    rillig 	UNION_TAG,
    203   1.65    rillig 	ENUM_TAG,
    204    1.1       cgd 	MOS,		/* member of struct */
    205    1.1       cgd 	MOU,		/* member of union */
    206  1.146    rillig 	BOOL_CONST,
    207  1.146    rillig 	ENUM_CONST,
    208    1.1       cgd 	ABSTRACT,	/* abstract symbol (sizeof, casts, unnamed argument) */
    209  1.136    rillig 	OLD_STYLE_ARG,	/* old-style function argument declarations */
    210   1.65    rillig 	PROTO_ARG,	/* used in declaration stack during prototype
    211    1.1       cgd 			   declaration */
    212    1.4       jpo 	INLINE		/* only used by the parser */
    213    1.1       cgd } scl_t;
    214    1.1       cgd 
    215    1.1       cgd /*
    216    1.1       cgd  * symbol table entry
    217    1.1       cgd  */
    218    1.1       cgd typedef	struct sym {
    219   1.33    rillig 	const	char *s_name;
    220    1.9       cgd 	const	char *s_rename;	/* renamed symbol's given name */
    221   1.44    rillig 	pos_t	s_def_pos;	/* position of last (prototype) definition,
    222   1.39    rillig 				   prototype declaration, no-prototype-def.,
    223    1.1       cgd 				   tentative definition or declaration,
    224    1.1       cgd 				   in this order */
    225   1.66    rillig 	pos_t	s_set_pos;	/* position of first initialization */
    226   1.44    rillig 	pos_t	s_use_pos;	/* position of first use */
    227    1.1       cgd 	symt_t	s_kind;		/* type of symbol */
    228  1.133    rillig 	const struct keyword *s_keyword;
    229  1.132    rillig 	bool	s_bitfield:1;
    230  1.132    rillig 	bool	s_set:1;	/* variable set, label defined */
    231  1.132    rillig 	bool	s_used:1;	/* variable/label used */
    232  1.132    rillig 	bool	s_arg:1;	/* symbol is function argument */
    233  1.141    rillig 	bool	s_register:1;	/* symbol is register variable */
    234  1.132    rillig 	bool	s_defarg:1;	/* undefined symbol in old style function
    235    1.1       cgd 				   definition */
    236  1.132    rillig 	bool	s_return_type_implicit_int:1;
    237  1.132    rillig 	bool	s_osdef:1;	/* symbol stems from old style function def. */
    238  1.132    rillig 	bool	s_inline:1;	/* true if this is an inline function */
    239  1.141    rillig 	struct	sym *s_ext_sym;	/* for locally declared external symbols, the
    240  1.141    rillig 				 * pointer to the external symbol with the
    241  1.141    rillig 				 * same name */
    242    1.1       cgd 	def_t	s_def;		/* declared, tentative defined, defined */
    243    1.1       cgd 	scl_t	s_scl;		/* storage class */
    244   1.71    rillig 	int	s_block_level;	/* level of declaration, -1 if not in symbol
    245    1.1       cgd 				   table */
    246   1.33    rillig 	type_t	*s_type;
    247    1.1       cgd 	union {
    248  1.147    rillig 		bool s_bool_constant;
    249  1.147    rillig 		int s_enum_constant;	/* XXX: should be TARG_INT */
    250  1.147    rillig 		struct {
    251  1.147    rillig 			/* XXX: what is the difference to s_type->t_str? */
    252  1.147    rillig 			struct_or_union	*sm_sou_type;
    253  1.147    rillig 			unsigned int sm_offset_in_bits;
    254  1.147    rillig 		} s_member;
    255  1.147    rillig 		struct {
    256  1.147    rillig 			int sk_token;
    257  1.147    rillig 			tspec_t	sk_tspec;	/* only for types */
    258  1.147    rillig 			tqual_t	sk_qualifier;	/* only for qualifiers */
    259  1.147    rillig 		} s_keyword;
    260  1.147    rillig 		struct	sym *s_old_style_args;	/* arguments in an old-style
    261  1.147    rillig 						 * function definition */
    262    1.1       cgd 	} u;
    263  1.139    rillig 	struct	sym *s_symtab_next;	/* next symbol with same hash value */
    264  1.139    rillig 	struct	sym **s_symtab_ref;	/* pointer to s_symtab_next of the
    265  1.139    rillig 					 * previous symbol */
    266   1.38    rillig 	struct	sym *s_next;	/* next struct/union member, enumerator,
    267    1.1       cgd 				   argument */
    268  1.139    rillig 	struct	sym *s_level_next;	/* next symbol declared on the same
    269  1.139    rillig 					 * level */
    270    1.1       cgd } sym_t;
    271    1.1       cgd 
    272    1.1       cgd /*
    273   1.35    rillig  * Used to keep some information about symbols before they are entered
    274    1.1       cgd  * into the symbol table.
    275    1.1       cgd  */
    276    1.1       cgd typedef	struct sbuf {
    277    1.1       cgd 	const	char *sb_name;		/* name of symbol */
    278    1.1       cgd 	size_t	sb_len;			/* length (without '\0') */
    279    1.1       cgd 	sym_t	*sb_sym;		/* symbol table entry */
    280    1.1       cgd } sbuf_t;
    281    1.1       cgd 
    282    1.1       cgd 
    283    1.1       cgd /*
    284    1.1       cgd  * tree node
    285    1.1       cgd  */
    286    1.1       cgd typedef	struct tnode {
    287    1.1       cgd 	op_t	tn_op;		/* operator */
    288    1.1       cgd 	type_t	*tn_type;	/* type */
    289  1.132    rillig 	bool	tn_lvalue:1;	/* node is lvalue */
    290  1.132    rillig 	bool	tn_cast:1;	/* if tn_op == CVT, it's an explicit cast */
    291  1.132    rillig 	bool	tn_parenthesized:1;
    292  1.132    rillig 	bool	tn_sys:1;	/* in strict bool mode, allow mixture between
    293  1.131    rillig 				 * bool and scalar, for code from system
    294  1.131    rillig 				 * headers that may be a mixture between
    295  1.131    rillig 				 * scalar types and bool
    296  1.110    rillig 				 */
    297  1.132    rillig 	bool	tn_system_dependent:1; /* depends on sizeof or offsetof */
    298    1.1       cgd 	union {
    299    1.1       cgd 		struct {
    300    1.1       cgd 			struct	tnode *_tn_left;	/* (left) operand */
    301    1.1       cgd 			struct	tnode *_tn_right;	/* right operand */
    302    1.1       cgd 		} tn_s;
    303    1.1       cgd 		sym_t	*_tn_sym;	/* symbol if op == NAME */
    304    1.1       cgd 		val_t	*_tn_val;	/* value if op == CON */
    305   1.42    rillig 		strg_t	*_tn_string;	/* string if op == STRING */
    306    1.1       cgd 	} tn_u;
    307    1.1       cgd } tnode_t;
    308    1.1       cgd 
    309   1.98    rillig #define	tn_left		tn_u.tn_s._tn_left
    310   1.98    rillig #define tn_right	tn_u.tn_s._tn_right
    311   1.98    rillig #define tn_sym		tn_u._tn_sym
    312   1.98    rillig #define	tn_val		tn_u._tn_val
    313   1.42    rillig #define	tn_string	tn_u._tn_string
    314    1.1       cgd 
    315  1.112    rillig struct generic_association {
    316  1.112    rillig 	type_t *ga_arg;		/* NULL means default or error */
    317  1.112    rillig 	tnode_t *ga_result;	/* NULL means error */
    318  1.112    rillig 	struct generic_association *ga_prev;
    319  1.107    rillig };
    320  1.107    rillig 
    321  1.129    rillig struct array_size {
    322  1.129    rillig 	bool has_dim;
    323  1.129    rillig 	int dim;
    324  1.129    rillig };
    325  1.129    rillig 
    326    1.1       cgd /*
    327  1.136    rillig  * For nested declarations there is a stack that holds all information
    328   1.76    rillig  * needed for the current level. dcs points to the innermost element of this
    329    1.1       cgd  * stack.
    330    1.1       cgd  *
    331   1.73    rillig  * d_ctx describes the context of the current declaration. Its value is
    332    1.1       cgd  * one of
    333   1.65    rillig  *	EXTERN		global declarations
    334   1.65    rillig  *	MOS or MOU	declarations of struct or union members
    335   1.76    rillig  *	CTCONST		declarations of enums or boolean constants
    336  1.136    rillig  *	OLD_STYLE_ARG	declaration of arguments in old-style function
    337   1.65    rillig  *			definitions
    338   1.65    rillig  *	PROTO_ARG	declaration of arguments in function prototypes
    339   1.65    rillig  *	AUTO		declaration of local symbols
    340   1.65    rillig  *	ABSTRACT	abstract declarations (sizeof, casts)
    341    1.1       cgd  *
    342    1.1       cgd  */
    343    1.1       cgd typedef	struct dinfo {
    344   1.72    rillig 	tspec_t	d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
    345   1.72    rillig 	tspec_t	d_complex_mod;	/* FLOAT or DOUBLE */
    346   1.72    rillig 	tspec_t	d_sign_mod;	/* SIGNED or UNSIGN */
    347   1.72    rillig 	tspec_t	d_rank_mod;	/* SHORT, LONG or QUAD */
    348    1.3       jpo 	scl_t	d_scl;		/* storage class */
    349  1.114    rillig 	type_t	*d_type;	/* after end_type() pointer to the type used
    350    1.1       cgd 				   for all declarators */
    351   1.78    rillig 	sym_t	*d_redeclared_symbol;
    352  1.147    rillig 	unsigned int d_offset;	/* offset of next structure member in bits */
    353  1.126    rillig 	unsigned short d_sou_align_in_bits; /* alignment required for current
    354  1.111    rillig 				 * structure */
    355    1.3       jpo 	scl_t	d_ctx;		/* context of declaration */
    356  1.132    rillig 	bool	d_const:1;	/* const in declaration specifiers */
    357  1.132    rillig 	bool	d_volatile:1;	/* volatile in declaration specifiers */
    358  1.132    rillig 	bool	d_inline:1;	/* inline in declaration specifiers */
    359  1.132    rillig 	bool	d_multiple_storage_classes:1; /* reported in end_type */
    360  1.132    rillig 	bool	d_invalid_type_combination:1;
    361  1.132    rillig 	bool	d_nonempty_decl:1; /* if at least one tag is declared
    362   1.79    rillig 				 * ... in the current function decl. */
    363  1.132    rillig 	bool	d_vararg:1;
    364  1.132    rillig 	bool	d_proto:1;	/* current function decl. is prototype */
    365  1.132    rillig 	bool	d_notyp:1;	/* set if no type specifier was present */
    366  1.132    rillig 	bool	d_asm:1;	/* set if d_ctx == AUTO and asm() present */
    367  1.132    rillig 	bool	d_packed:1;
    368  1.132    rillig 	bool	d_used:1;
    369    1.3       jpo 	type_t	*d_tagtyp;	/* tag during member declaration */
    370   1.80    rillig 	sym_t	*d_func_args;	/* list of arguments during function def. */
    371   1.80    rillig 	pos_t	d_func_def_pos;	/* position of function definition */
    372    1.3       jpo 	sym_t	*d_dlsyms;	/* first symbol declared at this level */
    373  1.139    rillig 	sym_t	**d_ldlsym;	/* points to s_level_next in the last symbol
    374  1.139    rillig 				   declaration at this level */
    375   1.80    rillig 	sym_t	*d_func_proto_syms; /* symbols defined in prototype */
    376  1.143    rillig 	struct dinfo *d_enclosing; /* the enclosing declaration level */
    377    1.1       cgd } dinfo_t;
    378    1.1       cgd 
    379  1.108    rillig /* One level of pointer indirection in declarators, including qualifiers. */
    380  1.108    rillig typedef	struct qual_ptr {
    381  1.106    rillig 	bool	p_const: 1;
    382  1.106    rillig 	bool	p_volatile: 1;
    383  1.108    rillig 	bool	p_pointer: 1;
    384  1.108    rillig 	struct	qual_ptr *p_next;
    385  1.108    rillig } qual_ptr;
    386    1.1       cgd 
    387    1.1       cgd /*
    388   1.93    rillig  * The values of the 'case' labels, linked via cl_next in reverse order of
    389   1.93    rillig  * appearance in the code, that is from bottom to top.
    390    1.1       cgd  */
    391   1.77    rillig typedef	struct case_label {
    392    1.1       cgd 	val_t	cl_val;
    393   1.77    rillig 	struct case_label *cl_next;
    394   1.77    rillig } case_label_t;
    395    1.1       cgd 
    396   1.88    rillig typedef enum {
    397   1.88    rillig 	CS_DO_WHILE,
    398   1.88    rillig 	CS_FOR,
    399   1.88    rillig 	CS_FUNCTION_BODY,
    400   1.88    rillig 	CS_IF,
    401   1.88    rillig 	CS_SWITCH,
    402   1.88    rillig 	CS_WHILE
    403   1.88    rillig } control_statement_kind;
    404   1.88    rillig 
    405    1.1       cgd /*
    406   1.35    rillig  * Used to keep information about nested control statements.
    407    1.1       cgd  */
    408   1.52    rillig typedef struct control_statement {
    409   1.88    rillig 	control_statement_kind c_kind;	/* to ensure proper nesting */
    410  1.132    rillig 	bool	c_loop:1;		/* 'continue' and 'break' are valid */
    411  1.132    rillig 	bool	c_switch:1;		/* 'case' and 'break' are valid */
    412  1.132    rillig 	bool	c_break:1;		/* the loop/switch has a reachable
    413   1.89    rillig 					 * 'break' statement */
    414  1.132    rillig 	bool	c_continue:1;		/* the loop has a reachable 'continue'
    415   1.89    rillig 					 * statement */
    416  1.132    rillig 	bool	c_default:1;		/* the switch has a 'default' label */
    417  1.132    rillig 	bool	c_maybe_endless:1;	/* the controlling expression is
    418   1.82    rillig 					 * always true (as in 'for (;;)' or
    419   1.82    rillig 					 * 'while (1)'), there may be break
    420   1.82    rillig 					 * statements though */
    421  1.132    rillig 	bool	c_always_then:1;
    422  1.132    rillig 	bool	c_reached_end_of_then:1;
    423  1.132    rillig 	bool	c_had_return_noval:1;	/* had "return;" */
    424  1.132    rillig 	bool	c_had_return_value:1;	/* had "return expr;" */
    425   1.89    rillig 
    426   1.89    rillig 	type_t	*c_switch_type;		/* type of switch expression */
    427  1.101    rillig 	tnode_t	*c_switch_expr;
    428   1.77    rillig 	case_label_t *c_case_labels;	/* list of case values */
    429   1.89    rillig 
    430   1.91    rillig 	struct	memory_block *c_for_expr3_mem; /* saved memory for end of loop
    431   1.89    rillig 					 * expression in for() */
    432   1.89    rillig 	tnode_t	*c_for_expr3;		/* end of loop expr in for() */
    433   1.89    rillig 	pos_t	c_for_expr3_pos;	/* position of end of loop expr */
    434   1.89    rillig 	pos_t	c_for_expr3_csrc_pos;	/* same for csrc_pos */
    435   1.89    rillig 
    436  1.118    rillig 	struct control_statement *c_surrounding;
    437  1.118    rillig } control_statement;
    438    1.1       cgd 
    439   1.18  christos typedef struct {
    440   1.70    rillig 	size_t lo;			/* inclusive */
    441   1.70    rillig 	size_t hi;			/* inclusive */
    442   1.18  christos } range_t;
    443   1.18  christos 
    444    1.1       cgd #include "externs1.h"
    445   1.11  augustss 
    446   1.11  augustss #define	ERR_SETSIZE	1024
    447   1.11  augustss #define __NERRBITS (sizeof(unsigned int))
    448   1.11  augustss 
    449   1.11  augustss typedef	struct err_set {
    450   1.11  augustss 	unsigned int	errs_bits[(ERR_SETSIZE + __NERRBITS-1) / __NERRBITS];
    451   1.11  augustss } err_set;
    452   1.11  augustss 
    453   1.11  augustss #define	ERR_SET(n, p)	\
    454   1.34    rillig 	((p)->errs_bits[(n)/__NERRBITS] |= (1 << ((n) % __NERRBITS)))
    455   1.11  augustss #define	ERR_CLR(n, p)	\
    456   1.34    rillig 	((p)->errs_bits[(n)/__NERRBITS] &= ~(1 << ((n) % __NERRBITS)))
    457   1.11  augustss #define	ERR_ISSET(n, p)	\
    458   1.55    rillig 	(((p)->errs_bits[(n)/__NERRBITS] & (1 << ((n) % __NERRBITS))) != 0)
    459   1.11  augustss #define	ERR_ZERO(p)	(void)memset((p), 0, sizeof(*(p)))
    460   1.14  christos 
    461   1.90    rillig #define INTERNAL_ERROR(fmt, args...) \
    462   1.90    rillig 	internal_error(__FILE__, __LINE__, fmt, ##args)
    463   1.11  augustss 
    464   1.37    rillig #define lint_assert(cond)						\
    465   1.37    rillig 	do {								\
    466   1.37    rillig 		if (!(cond))						\
    467   1.37    rillig 			assert_failed(__FILE__, __LINE__, __func__, #cond); \
    468  1.100    rillig 	} while (false)
    469   1.37    rillig 
    470   1.11  augustss extern err_set	msgset;
    471   1.49    rillig 
    472   1.49    rillig 
    473   1.49    rillig #ifdef DEBUG
    474   1.49    rillig #  include "err-msgs.h"
    475   1.49    rillig 
    476   1.49    rillig /* ARGSUSED */
    477  1.135    rillig static inline void __attribute__((format(printf, 1, 2)))
    478   1.49    rillig check_printf(const char *fmt, ...)
    479   1.49    rillig {
    480   1.49    rillig }
    481   1.49    rillig 
    482   1.99    rillig #  define wrap_check_printf_at(func, msgid, pos, args...)		\
    483   1.97    rillig 	do {								\
    484   1.99    rillig 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
    485   1.99    rillig 		(func)(msgid, pos, ##args);				\
    486  1.100    rillig 	} while (false)
    487   1.97    rillig 
    488   1.99    rillig #  define error_at(msgid, pos, args...) \
    489   1.99    rillig 	wrap_check_printf_at(error_at, msgid, pos, ##args)
    490   1.99    rillig #  define warning_at(msgid, pos, args...) \
    491   1.99    rillig 	wrap_check_printf_at(warning_at, msgid, pos, ##args)
    492   1.99    rillig #  define message_at(msgid, pos, args...) \
    493   1.99    rillig 	wrap_check_printf_at(message_at, msgid, pos, ##args)
    494   1.97    rillig 
    495   1.99    rillig #  define wrap_check_printf(func, msgid, args...)			\
    496   1.49    rillig 	do {								\
    497   1.99    rillig 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
    498   1.99    rillig 		(func)(msgid, ##args);					\
    499  1.100    rillig 	} while (false)
    500   1.49    rillig 
    501   1.99    rillig #  define error(msgid, args...) wrap_check_printf(error, msgid, ##args)
    502   1.99    rillig #  define warning(msgid, args...) wrap_check_printf(warning, msgid, ##args)
    503   1.99    rillig #  define gnuism(msgid, args...) wrap_check_printf(gnuism, msgid, ##args)
    504   1.99    rillig #  define c99ism(msgid, args...) wrap_check_printf(c99ism, msgid, ##args)
    505   1.99    rillig #  define c11ism(msgid, args...) wrap_check_printf(c11ism, msgid, ##args)
    506   1.49    rillig #endif
    507   1.54    rillig 
    508  1.135    rillig static inline bool
    509   1.69    rillig is_nonzero_val(const val_t *val)
    510   1.54    rillig {
    511   1.69    rillig 	return is_floating(val->v_tspec)
    512   1.69    rillig 	    ? val->v_ldbl != 0.0
    513   1.69    rillig 	    : val->v_quad != 0;
    514   1.54    rillig }
    515   1.54    rillig 
    516  1.135    rillig static inline bool
    517   1.68    rillig constant_is_nonzero(const tnode_t *tn)
    518   1.54    rillig {
    519   1.54    rillig 	lint_assert(tn->tn_op == CON);
    520   1.69    rillig 	lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
    521   1.69    rillig 	return is_nonzero_val(tn->tn_val);
    522   1.54    rillig }
    523   1.87    rillig 
    524  1.135    rillig static inline bool
    525   1.87    rillig is_zero(const tnode_t *tn)
    526   1.87    rillig {
    527   1.87    rillig 	return tn != NULL && tn->tn_op == CON && !is_nonzero_val(tn->tn_val);
    528   1.87    rillig }
    529   1.87    rillig 
    530  1.135    rillig static inline bool
    531   1.87    rillig is_nonzero(const tnode_t *tn)
    532   1.87    rillig {
    533   1.87    rillig 	return tn != NULL && tn->tn_op == CON && is_nonzero_val(tn->tn_val);
    534   1.87    rillig }
    535  1.102    rillig 
    536  1.135    rillig static inline bool
    537  1.123    rillig is_binary(const tnode_t *tn)
    538  1.123    rillig {
    539  1.123    rillig 	return modtab[tn->tn_op].m_binary;
    540  1.123    rillig }
    541  1.123    rillig 
    542  1.135    rillig static inline uint64_t
    543  1.102    rillig bit(unsigned i)
    544  1.102    rillig {
    545  1.120    rillig 	/*
    546  1.120    rillig 	 * TODO: Add proper support for INT128.
    547  1.120    rillig 	 * This involves changing val_t to 128 bits.
    548  1.120    rillig 	 */
    549  1.120    rillig 	if (i >= 64)
    550  1.120    rillig 		return 0;	/* XXX: not correct for INT128 and UINT128 */
    551  1.120    rillig 
    552  1.102    rillig 	lint_assert(i < 64);
    553  1.102    rillig 	return (uint64_t)1 << i;
    554  1.102    rillig }
    555  1.102    rillig 
    556  1.135    rillig static inline bool
    557  1.124    rillig msb(int64_t q, tspec_t t)
    558  1.124    rillig {
    559  1.127    rillig 	return (q & bit((unsigned int)size_in_bits(t) - 1)) != 0;
    560  1.124    rillig }
    561  1.124    rillig 
    562  1.135    rillig static inline uint64_t
    563  1.102    rillig value_bits(unsigned bitsize)
    564  1.102    rillig {
    565  1.102    rillig 	lint_assert(bitsize > 0);
    566  1.102    rillig 
    567  1.102    rillig 	/* for long double (80 or 128), double _Complex (128) */
    568  1.102    rillig 	/*
    569  1.102    rillig 	 * XXX: double _Complex does not have 128 bits of precision,
    570  1.102    rillig 	 * therefore it should never be necessary to query the value bits
    571  1.102    rillig 	 * of such a type; see d_c99_complex_split.c to trigger this case.
    572  1.102    rillig 	 */
    573  1.102    rillig 	if (bitsize >= 64)
    574  1.102    rillig 		return ~((uint64_t)0);
    575  1.102    rillig 
    576  1.102    rillig 	return ~(~(uint64_t)0 << bitsize);
    577  1.102    rillig }
    578  1.103    rillig 
    579  1.103    rillig /* C99 6.7.8p7 */
    580  1.135    rillig static inline bool
    581  1.103    rillig is_struct_or_union(tspec_t t)
    582  1.103    rillig {
    583  1.103    rillig 	return t == STRUCT || t == UNION;
    584  1.103    rillig }
    585  1.148    rillig 
    586  1.148    rillig static inline bool
    587  1.148    rillig is_member(const sym_t *sym)
    588  1.148    rillig {
    589  1.148    rillig 	return sym->s_scl == MOS || sym->s_scl == MOU;
    590  1.148    rillig }
    591