Home | History | Annotate | Line # | Download | only in lint1
lint1.h revision 1.221
      1  1.221    rillig /* $NetBSD: lint1.h,v 1.221 2024/03/09 13:54:47 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.189    rillig  *	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.1       cgd #include "op.h"
     37   1.12        tv 
     38  1.159    rillig /*
     39  1.159    rillig  * A memory pool collects allocated objects that must be available until:
     40  1.159    rillig  * - the end of a block,
     41  1.159    rillig  * - the end of an expression, or
     42  1.159    rillig  * - the end of the translation unit.
     43  1.159    rillig  */
     44  1.159    rillig typedef struct memory_pool {
     45  1.194    rillig 	struct memory_pool_item {
     46  1.194    rillig 		void *p;
     47  1.194    rillig #ifdef DEBUG_MEM
     48  1.194    rillig 		size_t size;
     49  1.194    rillig 		const char *descr;
     50  1.194    rillig #endif
     51  1.194    rillig 	} *items;
     52  1.159    rillig 	size_t	len;
     53  1.159    rillig 	size_t	cap;
     54  1.159    rillig } memory_pool;
     55  1.159    rillig 
     56  1.154    rillig /* See saved_lwarn in cgram.y. */
     57   1.53    rillig #define LWARN_ALL	(-2)
     58   1.53    rillig #define LWARN_NONE	(-1)
     59   1.27  christos 
     60    1.1       cgd /*
     61    1.1       cgd  * Describes the position of a declaration or anything else.
     62   1.96    rillig  *
     63   1.96    rillig  * FIXME: Just a single file:lineno pair is not enough to accurately describe
     64   1.96    rillig  *  the position of a symbol.  The whole inclusion path at that point must be
     65   1.96    rillig  *  stored as well.  This makes a difference for symbols from included
     66   1.96    rillig  *  headers, see print_stack_trace.
     67    1.1       cgd  */
     68    1.1       cgd typedef struct {
     69  1.205    rillig 	const char *p_file;
     70    1.1       cgd 	int	p_line;
     71    1.7       cgd 	int	p_uniq;			/* uniquifier */
     72    1.1       cgd } pos_t;
     73    1.7       cgd 
     74  1.191    rillig typedef struct {
     75  1.195    rillig 	bool tq_const;
     76  1.195    rillig 	bool tq_restrict;
     77  1.195    rillig 	bool tq_volatile;
     78  1.195    rillig 	bool tq_atomic;
     79  1.191    rillig } type_qualifiers;
     80    1.1       cgd 
     81  1.193    rillig /* A bool, integer or floating-point value. */
     82    1.1       cgd typedef struct {
     83    1.1       cgd 	tspec_t	v_tspec;
     84  1.105    rillig 	/*
     85  1.105    rillig 	 * Set if an integer constant is unsigned only in C90 and later, but
     86  1.105    rillig 	 * not in traditional C.
     87  1.105    rillig 	 *
     88  1.202    rillig 	 * See the operators table in oper.c, columns "l r".
     89  1.105    rillig 	 */
     90  1.105    rillig 	bool	v_unsigned_since_c90;
     91  1.166    rillig 	bool	v_char_constant;
     92    1.1       cgd 	union {
     93  1.177    rillig 		int64_t		integer;
     94  1.177    rillig 		long double	floating;
     95  1.177    rillig 	} u;
     96    1.1       cgd } val_t;
     97    1.1       cgd 
     98  1.216    rillig typedef struct sym sym_t;
     99  1.216    rillig 
    100    1.1       cgd /*
    101   1.62    rillig  * Structures of type struct_or_union uniquely identify structures. This can't
    102  1.174    rillig  * be done in structures of type type_t, because these are copied if they must
    103  1.174    rillig  * be modified. So it would not be possible to check if two structures are
    104  1.174    rillig  * identical by comparing the pointers to the type structures.
    105    1.1       cgd  *
    106  1.174    rillig  * If the structure has no tag name, its first typedef name is used to identify
    107  1.174    rillig  * the structure in lint2.
    108    1.1       cgd  */
    109  1.205    rillig typedef struct {
    110  1.126    rillig 	unsigned int sou_size_in_bits;
    111  1.176    rillig 	unsigned int sou_align_in_bits;
    112  1.132    rillig 	bool	sou_incomplete:1;
    113  1.216    rillig 	sym_t	*sou_first_member;
    114  1.216    rillig 	sym_t	*sou_tag;
    115  1.216    rillig 	sym_t	*sou_first_typedef;
    116   1.62    rillig } struct_or_union;
    117    1.1       cgd 
    118  1.219    rillig /* The same as in struct_or_union, only for enums. */
    119  1.205    rillig typedef struct {
    120  1.132    rillig 	bool	en_incomplete:1;
    121  1.216    rillig 	sym_t	*en_first_enumerator;
    122  1.216    rillig 	sym_t	*en_tag;
    123  1.216    rillig 	sym_t	*en_first_typedef;
    124   1.63    rillig } enumeration;
    125    1.1       cgd 
    126  1.219    rillig /* The type of an expression, object or function. */
    127   1.94    rillig struct lint1_type {
    128    1.1       cgd 	tspec_t	t_tspec;	/* type specifier */
    129  1.132    rillig 	bool	t_incomplete_array:1;
    130  1.219    rillig 	bool	t_const:1;
    131  1.219    rillig 	bool	t_volatile:1;
    132  1.220    rillig 	bool	t_proto:1;	/* function prototype (u.params valid) */
    133  1.132    rillig 	bool	t_vararg:1;	/* prototype with '...' */
    134  1.132    rillig 	bool	t_typedef:1;	/* type defined with typedef */
    135  1.151    rillig 	bool	t_typeof:1;	/* type defined with GCC's __typeof__ */
    136  1.132    rillig 	bool	t_bitfield:1;
    137  1.109    rillig 	/*
    138  1.219    rillig 	 * Either the type is currently an enum (having t_tspec ENUM), or it
    139  1.219    rillig 	 * is an integer type (typically INT) that has been implicitly
    140  1.220    rillig 	 * converted from an enum type.  In both cases, u.enumer is valid.
    141  1.109    rillig 	 */
    142  1.132    rillig 	bool	t_is_enum:1;
    143  1.132    rillig 	bool	t_packed:1;
    144    1.1       cgd 	union {
    145  1.220    rillig 		int		dimension;	/* if ARRAY */
    146  1.220    rillig 		struct_or_union	*sou;
    147  1.220    rillig 		enumeration	*enumer;
    148  1.220    rillig 		sym_t		*params;	/* if t_proto */
    149  1.220    rillig 	} u;
    150  1.169    rillig 	unsigned int	t_bit_field_width:8;
    151  1.169    rillig 	unsigned int	t_bit_field_offset:24;
    152  1.205    rillig 	struct lint1_type *t_subt;	/*- element type (if ARRAY),
    153  1.169    rillig 					 * return value (if FUNC),
    154  1.169    rillig 					 * target type (if PTR) */
    155   1.17  christos };
    156    1.1       cgd 
    157  1.205    rillig typedef enum {
    158  1.209    rillig 	SK_VCFT,		/* variable, constant, function, type */
    159  1.209    rillig 	SK_MEMBER,		/* member of a struct or union */
    160  1.209    rillig 	SK_TAG,
    161  1.209    rillig 	SK_LABEL
    162  1.209    rillig } symbol_kind;
    163    1.1       cgd 
    164  1.219    rillig /* storage classes and related things */
    165    1.1       cgd typedef enum {
    166  1.203    rillig 	NO_SCL,
    167  1.204    rillig 	EXTERN,			/* external symbols (independent of decl_t) */
    168  1.204    rillig 	STATIC,			/* static symbols (local and global) */
    169  1.204    rillig 	AUTO,			/* automatic symbols (except register) */
    170  1.204    rillig 	REG,			/* register */
    171  1.219    rillig 	TYPEDEF,
    172  1.190    rillig 	THREAD_LOCAL,
    173   1.65    rillig 	STRUCT_TAG,
    174   1.65    rillig 	UNION_TAG,
    175   1.65    rillig 	ENUM_TAG,
    176  1.160    rillig 	STRUCT_MEMBER,
    177  1.160    rillig 	UNION_MEMBER,
    178  1.146    rillig 	BOOL_CONST,
    179  1.146    rillig 	ENUM_CONST,
    180  1.204    rillig 	ABSTRACT,		/* abstract symbol (sizeof, casts, unnamed
    181  1.204    rillig 				 * argument) */
    182    1.1       cgd } scl_t;
    183    1.1       cgd 
    184  1.186    rillig /* C23 6.7.4 */
    185  1.186    rillig typedef enum {
    186  1.187    rillig 	FS_INLINE,		/* since C99 */
    187  1.187    rillig 	FS_NORETURN,		/* since C11 */
    188  1.186    rillig } function_specifier;
    189  1.186    rillig 
    190  1.219    rillig /* A type, variable, keyword; basically anything that has a name. */
    191  1.216    rillig struct sym {
    192  1.205    rillig 	const char *s_name;
    193  1.205    rillig 	const char *s_rename;	/* renamed symbol's given name */
    194   1.44    rillig 	pos_t	s_def_pos;	/* position of last (prototype) definition,
    195  1.204    rillig 				 * prototype declaration, no-prototype-def.,
    196  1.204    rillig 				 * tentative definition or declaration, in this
    197  1.204    rillig 				 * order */
    198   1.66    rillig 	pos_t	s_set_pos;	/* position of first initialization */
    199   1.44    rillig 	pos_t	s_use_pos;	/* position of first use */
    200  1.209    rillig 	symbol_kind s_kind;
    201  1.133    rillig 	const struct keyword *s_keyword;
    202  1.132    rillig 	bool	s_bitfield:1;
    203  1.219    rillig 	bool	s_set:1;
    204  1.219    rillig 	bool	s_used:1;
    205  1.219    rillig 	bool	s_param:1;
    206  1.219    rillig 	bool	s_register:1;
    207  1.199    rillig 	bool	s_defparam:1;	/* undefined symbol in old-style function
    208  1.204    rillig 				 * definition */
    209  1.132    rillig 	bool	s_return_type_implicit_int:1;
    210  1.158    rillig 	bool	s_osdef:1;	/* symbol stems from old-style function def. */
    211  1.219    rillig 	bool	s_inline:1;
    212  1.219    rillig 	sym_t	*s_ext_sym;	/* for locally declared external symbols, the
    213  1.204    rillig 				 * pointer to the external symbol with the same
    214  1.204    rillig 				 * name */
    215    1.1       cgd 	def_t	s_def;		/* declared, tentative defined, defined */
    216  1.219    rillig 	scl_t	s_scl;
    217   1.71    rillig 	int	s_block_level;	/* level of declaration, -1 if not in symbol
    218  1.204    rillig 				 * table */
    219   1.33    rillig 	type_t	*s_type;
    220    1.1       cgd 	union {
    221  1.147    rillig 		bool s_bool_constant;
    222  1.219    rillig 		int s_enum_constant;
    223  1.147    rillig 		struct {
    224  1.205    rillig 			struct_or_union *sm_containing_type;
    225  1.147    rillig 			unsigned int sm_offset_in_bits;
    226  1.147    rillig 		} s_member;
    227  1.147    rillig 		struct {
    228  1.147    rillig 			int sk_token;
    229  1.186    rillig 			union {
    230  1.186    rillig 				/* if T_TYPE or T_STRUCT_OR_UNION */
    231  1.186    rillig 				tspec_t sk_tspec;
    232  1.186    rillig 				/* if T_QUAL */
    233  1.191    rillig 				type_qualifiers sk_type_qualifier;
    234  1.186    rillig 				/* if T_FUNCTION_SPECIFIER */
    235  1.186    rillig 				function_specifier function_specifier;
    236  1.186    rillig 			} u;
    237  1.147    rillig 		} s_keyword;
    238  1.219    rillig 		sym_t	*s_old_style_params;	/* parameters in an old-style
    239  1.147    rillig 						 * function definition */
    240    1.1       cgd 	} u;
    241  1.216    rillig 	sym_t	*s_symtab_next;	/* next symbol in the same symtab bucket */
    242  1.216    rillig 	sym_t	**s_symtab_ref;	/* pointer to s_symtab_next of the previous
    243  1.216    rillig 				 * symbol */
    244  1.216    rillig 	sym_t	*s_next;	/* next struct/union member, enumerator,
    245  1.204    rillig 				 * parameter */
    246  1.216    rillig 	sym_t	*s_level_next;	/* next symbol declared on the same level */
    247  1.216    rillig };
    248    1.1       cgd 
    249    1.1       cgd /*
    250   1.35    rillig  * Used to keep some information about symbols before they are entered
    251    1.1       cgd  * into the symbol table.
    252    1.1       cgd  */
    253  1.216    rillig typedef struct {
    254  1.205    rillig 	const char *sb_name;		/* name of symbol */
    255    1.1       cgd 	size_t	sb_len;			/* length (without '\0') */
    256    1.1       cgd 	sym_t	*sb_sym;		/* symbol table entry */
    257    1.1       cgd } sbuf_t;
    258    1.1       cgd 
    259    1.1       cgd 
    260  1.214    rillig typedef struct {
    261  1.214    rillig 	struct tnode *func;
    262  1.214    rillig 	struct tnode **args;
    263  1.214    rillig 	size_t args_len;
    264  1.214    rillig 	size_t args_cap;
    265  1.214    rillig } function_call;
    266  1.214    rillig 
    267  1.216    rillig typedef struct tnode tnode_t;
    268  1.216    rillig 
    269  1.216    rillig /* An expression, forming an abstract syntax tree. */
    270  1.216    rillig struct tnode {
    271  1.219    rillig 	op_t	tn_op;
    272  1.219    rillig 	type_t	*tn_type;
    273  1.219    rillig 	bool	tn_lvalue:1;
    274  1.132    rillig 	bool	tn_cast:1;	/* if tn_op == CVT, it's an explicit cast */
    275  1.132    rillig 	bool	tn_parenthesized:1;
    276  1.219    rillig 	bool	tn_sys:1;	/* comes from a system header */
    277  1.132    rillig 	bool	tn_system_dependent:1; /* depends on sizeof or offsetof */
    278    1.1       cgd 	union {
    279    1.1       cgd 		struct {
    280  1.221    rillig 			tnode_t *left;	/* (left) operand */
    281  1.221    rillig 			tnode_t *right;	/* right operand */
    282  1.221    rillig 		} ops;
    283  1.221    rillig 		sym_t	*sym;		/* if NAME */
    284  1.221    rillig 		val_t	value;		/* if CON */
    285  1.221    rillig 		buffer	*str_literals;	/* if STRING; for
    286  1.212    rillig 					 * character strings, 'data' points to
    287  1.212    rillig 					 * the concatenated string literals in
    288  1.212    rillig 					 * source form, and 'len' is the
    289  1.212    rillig 					 * length of the concatenation; for
    290  1.212    rillig 					 * wide strings, 'data' is NULL and
    291  1.212    rillig 					 * 'len' is the number of resulting
    292  1.212    rillig 					 * characters */
    293  1.221    rillig 		function_call *call;	/* if CALL or ICALL */
    294  1.221    rillig 	} u;
    295  1.216    rillig };
    296    1.1       cgd 
    297  1.112    rillig struct generic_association {
    298  1.112    rillig 	type_t *ga_arg;		/* NULL means default or error */
    299  1.112    rillig 	tnode_t *ga_result;	/* NULL means error */
    300  1.112    rillig 	struct generic_association *ga_prev;
    301  1.107    rillig };
    302  1.107    rillig 
    303  1.216    rillig typedef struct {
    304  1.129    rillig 	bool has_dim;
    305  1.129    rillig 	int dim;
    306  1.216    rillig } array_size;
    307  1.129    rillig 
    308  1.173    rillig typedef enum decl_level_kind {
    309  1.173    rillig 	DLK_EXTERN,		/* global types, variables or functions */
    310  1.219    rillig 	DLK_STRUCT,		/* struct members */
    311  1.219    rillig 	DLK_UNION,		/* union members */
    312  1.219    rillig 	DLK_ENUM,		/* enum constants */
    313  1.199    rillig 	DLK_OLD_STYLE_PARAMS,	/* parameters in an old-style function
    314  1.150    rillig 				 * definition */
    315  1.173    rillig 	DLK_PROTO_PARAMS,	/* parameters in a prototype function
    316  1.150    rillig 				 * definition */
    317  1.173    rillig 	DLK_AUTO,		/* local types or variables */
    318  1.173    rillig 	DLK_ABSTRACT		/* abstract (unnamed) declaration; type name;
    319  1.173    rillig 				 * used in casts and sizeof */
    320  1.173    rillig } decl_level_kind;
    321  1.150    rillig 
    322    1.1       cgd /*
    323  1.199    rillig  * A declaration level collects information for a declarator in a struct,
    324  1.199    rillig  * union or enum declaration, a parameter declaration list, or a plain
    325  1.199    rillig  * declaration in or outside a function body.
    326  1.171    rillig  *
    327  1.171    rillig  * For nested declarations, the global 'dcs' holds all information needed for
    328  1.171    rillig  * the current level, the outer levels are available via 'd_enclosing'.
    329    1.1       cgd  */
    330  1.205    rillig typedef struct decl_level {
    331  1.173    rillig 	decl_level_kind d_kind;
    332   1.72    rillig 	tspec_t	d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
    333   1.72    rillig 	tspec_t	d_complex_mod;	/* FLOAT or DOUBLE */
    334   1.72    rillig 	tspec_t	d_sign_mod;	/* SIGNED or UNSIGN */
    335  1.180    rillig 	tspec_t	d_rank_mod;	/* SHORT, LONG or LLONG */
    336    1.3       jpo 	scl_t	d_scl;		/* storage class */
    337  1.174    rillig 	type_t	*d_type;	/* after dcs_end_type, the pointer to the type
    338  1.174    rillig 				 * used for all declarators */
    339   1.78    rillig 	sym_t	*d_redeclared_symbol;
    340  1.178    rillig 	unsigned int d_sou_size_in_bits;	/* size of the structure or
    341  1.178    rillig 						 * union being built, without
    342  1.178    rillig 						 * trailing padding */
    343  1.178    rillig 	unsigned int d_sou_align_in_bits;	/* alignment of the structure
    344  1.178    rillig 						 * or union being built */
    345  1.192    rillig 	type_qualifiers d_qual;	/* in declaration specifiers */
    346  1.132    rillig 	bool	d_inline:1;	/* inline in declaration specifiers */
    347  1.157    rillig 	bool	d_multiple_storage_classes:1; /* reported in dcs_end_type */
    348  1.132    rillig 	bool	d_invalid_type_combination:1;
    349  1.174    rillig 	bool	d_nonempty_decl:1; /* in a function declaration, whether at
    350  1.174    rillig 				 * least one tag was declared */
    351  1.170    rillig 	bool	d_no_type_specifier:1;
    352  1.132    rillig 	bool	d_asm:1;	/* set if d_ctx == AUTO and asm() present */
    353  1.132    rillig 	bool	d_packed:1;
    354  1.132    rillig 	bool	d_used:1;
    355  1.220    rillig 	type_t	*d_tag_type;	/* during a member or enumerator declaration,
    356  1.220    rillig 				 * the tag type to which the member belongs */
    357  1.199    rillig 	sym_t	*d_func_params;	/* during a function declaration, the
    358  1.198    rillig 				 * parameters, stored in the enclosing level */
    359  1.174    rillig 	pos_t	d_func_def_pos;	/* position of the function definition */
    360  1.170    rillig 	sym_t	*d_first_dlsym;	/* first symbol declared at this level */
    361  1.170    rillig 	sym_t	**d_last_dlsym;	/* points to s_level_next in the last symbol
    362  1.139    rillig 				   declaration at this level */
    363  1.198    rillig 	sym_t	*d_func_proto_syms;	/* symbols defined in prototype, such
    364  1.198    rillig 					 * as tagged types or parameter names,
    365  1.199    rillig 					 * may overlap d_func_params */
    366  1.173    rillig 	struct decl_level *d_enclosing; /* the enclosing declaration level */
    367  1.173    rillig } decl_level;
    368    1.1       cgd 
    369  1.216    rillig typedef struct {
    370  1.196    rillig 	sym_t	*first;
    371  1.196    rillig 	bool	vararg:1;
    372  1.196    rillig 	bool	prototype:1;
    373  1.216    rillig } parameter_list;
    374  1.196    rillig 
    375  1.191    rillig /*
    376  1.191    rillig  * A sequence of asterisks and qualifiers, from right to left.  For example,
    377  1.191    rillig  * 'const ***volatile **const volatile' results in [c-v-, ----, --v-, ----,
    378  1.191    rillig  * ----].  The leftmost 'const' is not included in this list, it is stored in
    379  1.193    rillig  * dcs->d_qual instead.
    380  1.191    rillig  */
    381  1.205    rillig typedef struct qual_ptr {
    382  1.191    rillig 	type_qualifiers qualifiers;
    383  1.205    rillig 	struct qual_ptr *p_next;
    384  1.108    rillig } qual_ptr;
    385    1.1       cgd 
    386  1.218    rillig /* The values of the 'case' labels. */
    387  1.218    rillig typedef struct {
    388  1.218    rillig 	val_t	*vals;
    389  1.218    rillig 	size_t	len;
    390  1.218    rillig 	size_t	cap;
    391  1.218    rillig } case_labels;
    392    1.1       cgd 
    393   1.88    rillig typedef enum {
    394   1.88    rillig 	CS_DO_WHILE,
    395   1.88    rillig 	CS_FOR,
    396   1.88    rillig 	CS_FUNCTION_BODY,
    397   1.88    rillig 	CS_IF,
    398   1.88    rillig 	CS_SWITCH,
    399   1.88    rillig 	CS_WHILE
    400   1.88    rillig } control_statement_kind;
    401   1.88    rillig 
    402    1.1       cgd /*
    403   1.35    rillig  * Used to keep information about nested control statements.
    404    1.1       cgd  */
    405   1.52    rillig typedef struct control_statement {
    406   1.88    rillig 	control_statement_kind c_kind;	/* to ensure proper nesting */
    407  1.204    rillig 	bool	c_loop:1;	/* 'continue' and 'break' are valid */
    408  1.204    rillig 	bool	c_switch:1;	/* 'case' and 'break' are valid */
    409  1.204    rillig 	bool	c_break:1;	/* the loop/switch has a reachable 'break'
    410  1.204    rillig 				 * statement */
    411  1.204    rillig 	bool	c_continue:1;	/* the loop has a reachable 'continue'
    412  1.204    rillig 				 * statement */
    413  1.204    rillig 	bool	c_default:1;	/* the switch has a 'default' label */
    414  1.132    rillig 	bool	c_maybe_endless:1;	/* the controlling expression is
    415   1.82    rillig 					 * always true (as in 'for (;;)' or
    416   1.82    rillig 					 * 'while (1)'), there may be break
    417   1.82    rillig 					 * statements though */
    418  1.132    rillig 	bool	c_always_then:1;
    419  1.132    rillig 	bool	c_reached_end_of_then:1;
    420  1.132    rillig 	bool	c_had_return_noval:1;	/* had "return;" */
    421  1.132    rillig 	bool	c_had_return_value:1;	/* had "return expr;" */
    422   1.89    rillig 
    423  1.204    rillig 	type_t	*c_switch_type;	/* type of switch expression */
    424  1.101    rillig 	tnode_t	*c_switch_expr;
    425  1.218    rillig 	case_labels c_case_labels;	/* list of case values */
    426   1.89    rillig 
    427  1.159    rillig 	memory_pool c_for_expr3_mem;	/* saved memory for end of loop
    428   1.89    rillig 					 * expression in for() */
    429  1.204    rillig 	tnode_t	*c_for_expr3;	/* end of loop expr in for() */
    430   1.89    rillig 	pos_t	c_for_expr3_pos;	/* position of end of loop expr */
    431   1.89    rillig 	pos_t	c_for_expr3_csrc_pos;	/* same for csrc_pos */
    432   1.89    rillig 
    433  1.118    rillig 	struct control_statement *c_surrounding;
    434  1.118    rillig } control_statement;
    435    1.1       cgd 
    436   1.18  christos typedef struct {
    437  1.204    rillig 	size_t lo;		/* inclusive */
    438  1.204    rillig 	size_t hi;		/* inclusive */
    439   1.18  christos } range_t;
    440   1.18  christos 
    441  1.207    rillig typedef enum designator_kind {
    442  1.208    rillig 	DK_MEMBER,		/* .member */
    443  1.208    rillig 	DK_SUBSCRIPT,		/* [subscript] */
    444  1.207    rillig 	DK_SCALAR		/* no textual representation, not generated by
    445  1.208    rillig 				 * the parser; used for scalar initializer
    446  1.208    rillig 				 * expressions surrounded by braces */
    447  1.207    rillig } designator_kind;
    448  1.207    rillig 
    449  1.207    rillig /*
    450  1.207    rillig  * A single component on the path from the "current object" of a brace level
    451  1.207    rillig  * to the sub-object that is initialized by an expression.
    452  1.207    rillig  *
    453  1.207    rillig  * C99 6.7.8p6, 6.7.8p7
    454  1.207    rillig  */
    455  1.207    rillig typedef struct designator {
    456  1.207    rillig 	designator_kind	dr_kind;
    457  1.208    rillig 	const sym_t	*dr_member;	/* for DK_MEMBER */
    458  1.208    rillig 	size_t		dr_subscript;	/* for DK_SUBSCRIPT */
    459  1.207    rillig 	bool		dr_done;
    460  1.207    rillig } designator;
    461  1.207    rillig 
    462  1.207    rillig /*
    463  1.207    rillig  * The path from the "current object" of a brace level to the sub-object that
    464  1.207    rillig  * is initialized by an expression.  Examples of designations are '.member'
    465  1.207    rillig  * or '.member[123].member.member[1][1]'.
    466  1.207    rillig  *
    467  1.207    rillig  * C99 6.7.8p6, 6.7.8p7
    468  1.207    rillig  */
    469  1.207    rillig typedef struct designation {
    470  1.207    rillig 	designator	*dn_items;
    471  1.207    rillig 	size_t		dn_len;
    472  1.207    rillig 	size_t		dn_cap;
    473  1.207    rillig } designation;
    474  1.207    rillig 
    475  1.188    rillig typedef enum {
    476  1.188    rillig 	LC_ARGSUSED,
    477  1.188    rillig 	LC_BITFIELDTYPE,
    478  1.188    rillig 	LC_CONSTCOND,
    479  1.188    rillig 	LC_FALLTHROUGH,
    480  1.188    rillig 	LC_LINTLIBRARY,
    481  1.188    rillig 	LC_LINTED,
    482  1.188    rillig 	LC_LONGLONG,
    483  1.188    rillig 	LC_NOTREACHED,
    484  1.188    rillig 	LC_PRINTFLIKE,
    485  1.188    rillig 	LC_PROTOLIB,
    486  1.188    rillig 	LC_SCANFLIKE,
    487  1.188    rillig 	LC_VARARGS,
    488  1.188    rillig } lint_comment;
    489  1.188    rillig 
    490  1.212    rillig typedef struct {
    491  1.212    rillig 	size_t start;
    492  1.215    rillig 	size_t end;
    493  1.212    rillig 	uint64_t value;
    494  1.212    rillig 	bool escaped;		/* \n, \003, \x24 */
    495  1.212    rillig 	bool named_escape;	/* \a, \n, etc. */
    496  1.212    rillig 	bool literal_escape;	/* \?, \\, etc. */
    497  1.212    rillig 	uint8_t octal_digits;	/* 1 to 3; 0 means not applicable */
    498  1.212    rillig 	uint8_t hex_digits;	/* 1 to 3; 0 means not applicable */
    499  1.212    rillig 	bool next_literal;	/* when a new string literal begins */
    500  1.212    rillig 	bool invalid_escape;	/* single-character escape, recoverable */
    501  1.212    rillig 	bool overflow;		/* for octal and hex escapes */
    502  1.212    rillig 	bool missing_hex_digits;
    503  1.212    rillig 	bool unescaped_newline;	/* stops iterating */
    504  1.212    rillig } quoted_iterator;
    505  1.212    rillig 
    506    1.1       cgd #include "externs1.h"
    507   1.11  augustss 
    508   1.37    rillig #define lint_assert(cond)						\
    509   1.37    rillig 	do {								\
    510   1.37    rillig 		if (!(cond))						\
    511   1.37    rillig 			assert_failed(__FILE__, __LINE__, __func__, #cond); \
    512  1.100    rillig 	} while (false)
    513   1.37    rillig 
    514   1.49    rillig #ifdef DEBUG
    515   1.49    rillig #  include "err-msgs.h"
    516   1.49    rillig 
    517   1.49    rillig /* ARGSUSED */
    518  1.163    rillig static inline void __printflike(1, 2)
    519   1.49    rillig check_printf(const char *fmt, ...)
    520   1.49    rillig {
    521   1.49    rillig }
    522   1.49    rillig 
    523   1.99    rillig #  define wrap_check_printf_at(func, msgid, pos, args...)		\
    524   1.97    rillig 	do {								\
    525   1.99    rillig 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
    526   1.99    rillig 		(func)(msgid, pos, ##args);				\
    527  1.100    rillig 	} while (false)
    528   1.97    rillig 
    529   1.99    rillig #  define error_at(msgid, pos, args...) \
    530   1.99    rillig 	wrap_check_printf_at(error_at, msgid, pos, ##args)
    531   1.99    rillig #  define warning_at(msgid, pos, args...) \
    532   1.99    rillig 	wrap_check_printf_at(warning_at, msgid, pos, ##args)
    533   1.99    rillig #  define message_at(msgid, pos, args...) \
    534   1.99    rillig 	wrap_check_printf_at(message_at, msgid, pos, ##args)
    535   1.97    rillig 
    536  1.168    rillig #  define wrap_check_printf(func, cond, msgid, args...)			\
    537  1.152    rillig 	({								\
    538  1.168    rillig 		if (/* CONSTCOND */cond)				\
    539  1.168    rillig 			debug_step("%s:%d: %s %d '%s' in %s",		\
    540  1.168    rillig 			    __FILE__, __LINE__,	#func, msgid,		\
    541  1.168    rillig 			    __CONCAT(MSG_, msgid), __func__);		\
    542   1.99    rillig 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
    543   1.99    rillig 		(func)(msgid, ##args);					\
    544  1.152    rillig 		/* LINTED 129 */					\
    545  1.152    rillig 	})
    546   1.49    rillig 
    547  1.185    rillig #  define error(msgid, args...) wrap_check_printf(error, \
    548  1.185    rillig     true, msgid, ##args)
    549  1.185    rillig #  define warning(msgid, args...) wrap_check_printf(warning, \
    550  1.185    rillig     true, msgid, ##args)
    551  1.185    rillig #  define gnuism(msgid, args...) wrap_check_printf(gnuism, \
    552  1.185    rillig     !allow_gcc || (!allow_trad && !allow_c99), msgid, ##args)
    553  1.185    rillig #  define c99ism(msgid, args...) wrap_check_printf(c99ism, \
    554  1.185    rillig     !allow_c99 && (!allow_gcc || !allow_trad), msgid, ##args)
    555  1.185    rillig #  define c11ism(msgid, args...) wrap_check_printf(c11ism, \
    556  1.185    rillig     !allow_c11 && !allow_gcc, msgid, ##args)
    557  1.185    rillig #  define c23ism(msgid, args...) wrap_check_printf(c23ism, \
    558  1.185    rillig     !allow_c23, msgid, ##args)
    559   1.49    rillig #endif
    560   1.54    rillig 
    561  1.156    rillig #ifdef DEBUG
    562  1.156    rillig #  define query_message(query_id, args...)				\
    563  1.156    rillig 	do {								\
    564  1.168    rillig 		debug_step("%s:%d: query %d '%s' in %s",		\
    565  1.168    rillig 		    __FILE__, __LINE__,					\
    566  1.168    rillig 		    query_id, __CONCAT(MSG_Q, query_id), __func__);	\
    567  1.156    rillig 		check_printf(__CONCAT(MSG_Q, query_id), ##args);	\
    568  1.156    rillig 		(query_message)(query_id, ##args);			\
    569  1.156    rillig 	} while (false)
    570  1.156    rillig #else
    571  1.156    rillig #  define query_message(...)						\
    572  1.156    rillig 	do {								\
    573  1.156    rillig 		if (any_query_enabled)					\
    574  1.156    rillig 			(query_message)(__VA_ARGS__);			\
    575  1.156    rillig 	} while (false)
    576  1.156    rillig #endif
    577  1.156    rillig 
    578  1.172    rillig /* Copies curr_pos, keeping things unique. */
    579  1.172    rillig static inline pos_t
    580  1.172    rillig unique_curr_pos(void)
    581  1.172    rillig {
    582  1.172    rillig 	pos_t curr = curr_pos;
    583  1.172    rillig 	curr_pos.p_uniq++;
    584  1.172    rillig 	if (curr_pos.p_file == csrc_pos.p_file)
    585  1.172    rillig 		csrc_pos.p_uniq++;
    586  1.172    rillig 	return curr;
    587  1.172    rillig }
    588  1.172    rillig 
    589  1.135    rillig static inline bool
    590   1.69    rillig is_nonzero_val(const val_t *val)
    591   1.54    rillig {
    592   1.69    rillig 	return is_floating(val->v_tspec)
    593  1.177    rillig 	    ? val->u.floating != 0.0
    594  1.177    rillig 	    : val->u.integer != 0;
    595   1.54    rillig }
    596   1.54    rillig 
    597  1.135    rillig static inline bool
    598   1.68    rillig constant_is_nonzero(const tnode_t *tn)
    599   1.54    rillig {
    600   1.54    rillig 	lint_assert(tn->tn_op == CON);
    601  1.221    rillig 	lint_assert(tn->tn_type->t_tspec == tn->u.value.v_tspec);
    602  1.221    rillig 	return is_nonzero_val(&tn->u.value);
    603   1.54    rillig }
    604   1.87    rillig 
    605  1.135    rillig static inline bool
    606   1.87    rillig is_zero(const tnode_t *tn)
    607   1.87    rillig {
    608  1.221    rillig 	return tn != NULL && tn->tn_op == CON && !is_nonzero_val(&tn->u.value);
    609   1.87    rillig }
    610   1.87    rillig 
    611  1.135    rillig static inline bool
    612   1.87    rillig is_nonzero(const tnode_t *tn)
    613   1.87    rillig {
    614  1.221    rillig 	return tn != NULL && tn->tn_op == CON && is_nonzero_val(&tn->u.value);
    615   1.87    rillig }
    616  1.102    rillig 
    617  1.184    rillig static inline const char *
    618  1.184    rillig op_name(op_t op)
    619  1.184    rillig {
    620  1.184    rillig 	return modtab[op].m_name;
    621  1.184    rillig }
    622  1.184    rillig 
    623  1.135    rillig static inline bool
    624  1.123    rillig is_binary(const tnode_t *tn)
    625  1.123    rillig {
    626  1.123    rillig 	return modtab[tn->tn_op].m_binary;
    627  1.123    rillig }
    628  1.123    rillig 
    629  1.135    rillig static inline uint64_t
    630  1.102    rillig bit(unsigned i)
    631  1.102    rillig {
    632  1.120    rillig 	/*
    633  1.204    rillig 	 * TODO: Add proper support for INT128. This involves changing val_t to
    634  1.204    rillig 	 * 128 bits.
    635  1.120    rillig 	 */
    636  1.120    rillig 	if (i >= 64)
    637  1.120    rillig 		return 0;	/* XXX: not correct for INT128 and UINT128 */
    638  1.120    rillig 
    639  1.102    rillig 	lint_assert(i < 64);
    640  1.102    rillig 	return (uint64_t)1 << i;
    641  1.102    rillig }
    642  1.102    rillig 
    643  1.135    rillig static inline bool
    644  1.177    rillig msb(int64_t si, tspec_t t)
    645  1.124    rillig {
    646  1.182    rillig 	return ((uint64_t)si & bit(size_in_bits(t) - 1)) != 0;
    647  1.124    rillig }
    648  1.124    rillig 
    649  1.135    rillig static inline uint64_t
    650  1.102    rillig value_bits(unsigned bitsize)
    651  1.102    rillig {
    652  1.102    rillig 	lint_assert(bitsize > 0);
    653  1.102    rillig 
    654  1.102    rillig 	/* for long double (80 or 128), double _Complex (128) */
    655  1.102    rillig 	/*
    656  1.204    rillig 	 * XXX: double _Complex does not have 128 bits of precision, therefore
    657  1.204    rillig 	 * it should never be necessary to query the value bits of such a type;
    658  1.204    rillig 	 * see d_c99_complex_split.c to trigger this case.
    659  1.102    rillig 	 */
    660  1.102    rillig 	if (bitsize >= 64)
    661  1.206    rillig 		return ~(uint64_t)0;
    662  1.102    rillig 
    663  1.102    rillig 	return ~(~(uint64_t)0 << bitsize);
    664  1.102    rillig }
    665  1.103    rillig 
    666  1.103    rillig /* C99 6.7.8p7 */
    667  1.135    rillig static inline bool
    668  1.103    rillig is_struct_or_union(tspec_t t)
    669  1.103    rillig {
    670  1.103    rillig 	return t == STRUCT || t == UNION;
    671  1.103    rillig }
    672  1.148    rillig 
    673  1.148    rillig static inline bool
    674  1.148    rillig is_member(const sym_t *sym)
    675  1.148    rillig {
    676  1.160    rillig 	return sym->s_scl == STRUCT_MEMBER || sym->s_scl == UNION_MEMBER;
    677  1.148    rillig }
    678  1.183    rillig 
    679  1.183    rillig static inline void
    680  1.209    rillig set_sym_kind(symbol_kind kind)
    681  1.183    rillig {
    682  1.183    rillig 	if (yflag)
    683  1.183    rillig 		debug_step("%s: %s -> %s", __func__,
    684  1.209    rillig 		    symbol_kind_name(sym_kind), symbol_kind_name(kind));
    685  1.209    rillig 	sym_kind = kind;
    686  1.183    rillig }
    687