xref: /netbsd/usr.bin/xlint/lint1/lint1.h (revision 59a8babc)
1 /* $NetBSD: lint1.h,v 1.197 2023/07/29 10:34:24 rillig 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  * A memory pool collects allocated objects that must be available until:
40  * - the end of a block,
41  * - the end of an expression, or
42  * - the end of the translation unit.
43  */
44 typedef struct memory_pool {
45 	struct memory_pool_item {
46 		void *p;
47 #ifdef DEBUG_MEM
48 		size_t size;
49 		const char *descr;
50 #endif
51 	} *items;
52 	size_t	len;
53 	size_t	cap;
54 } memory_pool;
55 
56 /* See saved_lwarn in cgram.y. */
57 #define LWARN_ALL	(-2)
58 #define LWARN_NONE	(-1)
59 
60 /*
61  * Describes the position of a declaration or anything else.
62  *
63  * FIXME: Just a single file:lineno pair is not enough to accurately describe
64  *  the position of a symbol.  The whole inclusion path at that point must be
65  *  stored as well.  This makes a difference for symbols from included
66  *  headers, see print_stack_trace.
67  */
68 typedef struct {
69 	const	char *p_file;
70 	int	p_line;
71 	int	p_uniq;			/* uniquifier */
72 } pos_t;
73 
74 /*
75  * Strings cannot be referenced simply by a pointer to their first
76  * char. This is because strings can contain NUL characters other than the
77  * trailing NUL.
78  *
79  * Strings are stored with a trailing NUL.
80  */
81 typedef	struct strg {
82 	bool	st_char;	/* string doesn't have an 'L' prefix */
83 	size_t	st_len;		/* length without trailing NUL */
84 	void	*st_mem;	/* char[] for st_char, or wchar_t[] */
85 } strg_t;
86 
87 // TODO: Use bit-fields instead of plain bool, but keep an eye on arm and
88 // powerpc, on which GCC 10.5.0 generates code that leads to extra 327
89 // warnings, even in msg_327.c, which does not contain any type qualifier at
90 // all.  A possible starting point for continuing the investigation is that
91 // type_qualifiers is a very small struct that contains only bool bit-fields,
92 // and this struct is a member of the parser's union.
93 //
94 // Instead of using plain bool instead of bit-fields, an alternative workaround
95 // is to compile cgram.c with -Os or -O1 instead of -O2.  The generated code
96 // between -Os and -O2 differs too much though to give a hint at the root
97 // cause.
98 typedef struct {
99 	bool tq_const;
100 	bool tq_restrict;
101 	bool tq_volatile;
102 	bool tq_atomic;
103 } type_qualifiers;
104 
105 /* A bool, integer or floating-point value. */
106 typedef struct {
107 	tspec_t	v_tspec;
108 	/*
109 	 * Set if an integer constant is unsigned only in C90 and later, but
110 	 * not in traditional C.
111 	 *
112 	 * See the operators table in ops.def, columns "l r".
113 	 */
114 	bool	v_unsigned_since_c90;
115 	bool	v_char_constant;
116 	union {
117 		int64_t		integer;
118 		long double	floating;
119 	} u;
120 } val_t;
121 
122 /*
123  * Structures of type struct_or_union uniquely identify structures. This can't
124  * be done in structures of type type_t, because these are copied if they must
125  * be modified. So it would not be possible to check if two structures are
126  * identical by comparing the pointers to the type structures.
127  *
128  * If the structure has no tag name, its first typedef name is used to identify
129  * the structure in lint2.
130  */
131 typedef	struct {
132 	unsigned int sou_size_in_bits;
133 	unsigned int sou_align_in_bits;
134 	bool	sou_incomplete:1;
135 	struct	sym *sou_first_member;
136 	struct	sym *sou_tag;
137 	struct	sym *sou_first_typedef;
138 } struct_or_union;
139 
140 /*
141  * same as above for enums
142  */
143 typedef	struct {
144 	bool	en_incomplete:1;
145 	struct	sym *en_first_enumerator;
146 	struct	sym *en_tag;
147 	struct	sym *en_first_typedef;
148 } enumeration;
149 
150 /*
151  * The type of an expression or object. Complex types are formed via t_subt
152  * (for arrays, pointers and functions), as well as t_sou.
153  */
154 struct lint1_type {
155 	tspec_t	t_tspec;	/* type specifier */
156 	bool	t_incomplete_array:1;
157 	bool	t_const:1;	/* const modifier */
158 	bool	t_volatile:1;	/* volatile modifier */
159 	bool	t_proto:1;	/* function prototype (t_args valid) */
160 	bool	t_vararg:1;	/* prototype with '...' */
161 	bool	t_typedef:1;	/* type defined with typedef */
162 	bool	t_typeof:1;	/* type defined with GCC's __typeof__ */
163 	bool	t_bitfield:1;
164 	/*
165 	 * Either the type is currently an enum (having t_tspec ENUM), or
166 	 * it is an integer type (typically INT) that has been implicitly
167 	 * converted from an enum type.  In both cases, t_enum is valid.
168 	 *
169 	 * The information about a former enum type is retained to allow
170 	 * type checks in expressions such as ((var1 & 0x0001) == var2), to
171 	 * detect when var1 and var2 are from incompatible enum types.
172 	 */
173 	bool	t_is_enum:1;
174 	bool	t_packed:1;
175 	union {
176 		int	_t_dim;		/* dimension (if ARRAY) */
177 		struct_or_union	*_t_sou;
178 		enumeration	*_t_enum;
179 		struct	sym *_t_args;	/* arguments (if t_proto) */
180 	} t_u;
181 	unsigned int	t_bit_field_width:8;
182 	unsigned int	t_bit_field_offset:24;
183 	struct	lint1_type *t_subt;	/* element type (if ARRAY),
184 					 * return value (if FUNC),
185 					 * target type (if PTR) */
186 };
187 
188 #define	t_dim	t_u._t_dim
189 #define	t_sou	t_u._t_sou
190 #define	t_enum	t_u._t_enum
191 #define	t_args	t_u._t_args
192 
193 /*
194  * types of symbols
195  */
196 typedef	enum {
197 	FVFT,		/* variables, functions, type names, enums */
198 	FMEMBER,	/* members of structs or unions */
199 	FTAG,		/* tags */
200 	FLABEL		/* labels */
201 } symt_t;
202 
203 /*
204  * storage classes and related things
205  */
206 typedef enum {
207 	NOSCL,
208 	EXTERN,		/* external symbols (independent of decl_t) */
209 	STATIC,		/* static symbols (local and global) */
210 	AUTO,		/* automatic symbols (except register) */
211 	REG,		/* register */
212 	TYPEDEF,	/* typedef */
213 	THREAD_LOCAL,
214 	STRUCT_TAG,
215 	UNION_TAG,
216 	ENUM_TAG,
217 	STRUCT_MEMBER,
218 	UNION_MEMBER,
219 	BOOL_CONST,
220 	ENUM_CONST,
221 	ABSTRACT,	/* abstract symbol (sizeof, casts, unnamed argument) */
222 } scl_t;
223 
224 /* C23 6.7.4 */
225 typedef enum {
226 	FS_INLINE,		/* since C99 */
227 	FS_NORETURN,		/* since C11 */
228 } function_specifier;
229 
230 /*
231  * symbol table entry
232  */
233 typedef	struct sym {
234 	const	char *s_name;
235 	const	char *s_rename;	/* renamed symbol's given name */
236 	pos_t	s_def_pos;	/* position of last (prototype) definition,
237 				   prototype declaration, no-prototype-def.,
238 				   tentative definition or declaration,
239 				   in this order */
240 	pos_t	s_set_pos;	/* position of first initialization */
241 	pos_t	s_use_pos;	/* position of first use */
242 	symt_t	s_kind;		/* type of symbol */
243 	const struct keyword *s_keyword;
244 	bool	s_bitfield:1;
245 	bool	s_set:1;	/* variable set, label defined */
246 	bool	s_used:1;	/* variable/label used */
247 	bool	s_arg:1;	/* symbol is function argument */
248 	bool	s_register:1;	/* symbol is register variable */
249 	bool	s_defarg:1;	/* undefined symbol in old-style function
250 				   definition */
251 	bool	s_return_type_implicit_int:1;
252 	bool	s_osdef:1;	/* symbol stems from old-style function def. */
253 	bool	s_inline:1;	/* true if this is an inline function */
254 	struct	sym *s_ext_sym;	/* for locally declared external symbols, the
255 				 * pointer to the external symbol with the
256 				 * same name */
257 	def_t	s_def;		/* declared, tentative defined, defined */
258 	scl_t	s_scl;		/* storage class, more or less */
259 	int	s_block_level;	/* level of declaration, -1 if not in symbol
260 				   table */
261 	type_t	*s_type;
262 	union {
263 		bool s_bool_constant;
264 		int s_enum_constant;	/* XXX: should be TARG_INT */
265 		struct {
266 			struct_or_union	*sm_containing_type;
267 			unsigned int sm_offset_in_bits;
268 		} s_member;
269 		struct {
270 			int sk_token;
271 			union {
272 				/* if T_TYPE or T_STRUCT_OR_UNION */
273 				tspec_t sk_tspec;
274 				/* if T_QUAL */
275 				type_qualifiers sk_type_qualifier;
276 				/* if T_FUNCTION_SPECIFIER */
277 				function_specifier function_specifier;
278 			} u;
279 		} s_keyword;
280 		struct	sym *s_old_style_args;	/* arguments in an old-style
281 						 * function definition */
282 	} u;
283 	struct	sym *s_symtab_next;	/* next symbol with same hash value */
284 	struct	sym **s_symtab_ref;	/* pointer to s_symtab_next of the
285 					 * previous symbol */
286 	struct	sym *s_next;	/* next struct/union member, enumerator,
287 				   argument */
288 	struct	sym *s_level_next;	/* next symbol declared on the same
289 					 * level */
290 } sym_t;
291 
292 /*
293  * Used to keep some information about symbols before they are entered
294  * into the symbol table.
295  */
296 typedef	struct sbuf {
297 	const	char *sb_name;		/* name of symbol */
298 	size_t	sb_len;			/* length (without '\0') */
299 	sym_t	*sb_sym;		/* symbol table entry */
300 } sbuf_t;
301 
302 
303 /*
304  * tree node
305  */
306 typedef	struct tnode {
307 	op_t	tn_op;		/* operator */
308 	type_t	*tn_type;	/* type */
309 	bool	tn_lvalue:1;	/* node is lvalue */
310 	bool	tn_cast:1;	/* if tn_op == CVT, it's an explicit cast */
311 	bool	tn_parenthesized:1;
312 	bool	tn_sys:1;	/* the operator comes from a system header;
313 				 * used in strict bool mode to allow mixing
314 				 * bool and scalar, as these places are not
315 				 * considered fixable */
316 	bool	tn_system_dependent:1; /* depends on sizeof or offsetof */
317 	union {
318 		struct {
319 			struct	tnode *_tn_left;	/* (left) operand */
320 			struct	tnode *_tn_right;	/* right operand */
321 		} tn_s;
322 		sym_t	*_tn_sym;	/* symbol if op == NAME */
323 		val_t	_tn_val;	/* value if op == CON */
324 		strg_t	*_tn_string;	/* string if op == STRING */
325 	} tn_u;
326 } tnode_t;
327 
328 #define	tn_left		tn_u.tn_s._tn_left
329 #define tn_right	tn_u.tn_s._tn_right
330 #define tn_sym		tn_u._tn_sym
331 #define	tn_val		tn_u._tn_val
332 #define	tn_string	tn_u._tn_string
333 
334 struct generic_association {
335 	type_t *ga_arg;		/* NULL means default or error */
336 	tnode_t *ga_result;	/* NULL means error */
337 	struct generic_association *ga_prev;
338 };
339 
340 struct array_size {
341 	bool has_dim;
342 	int dim;
343 };
344 
345 typedef enum decl_level_kind {
346 	DLK_EXTERN,		/* global types, variables or functions */
347 	DLK_STRUCT,		/* members */
348 	DLK_UNION,		/* members */
349 	DLK_ENUM,		/* constants */
350 	DLK_OLD_STYLE_ARGS,	/* arguments in an old-style function
351 				 * definition */
352 	DLK_PROTO_PARAMS,	/* parameters in a prototype function
353 				 * definition */
354 	DLK_AUTO,		/* local types or variables */
355 	DLK_ABSTRACT		/* abstract (unnamed) declaration; type name;
356 				 * used in casts and sizeof */
357 } decl_level_kind;
358 
359 /*
360  * A declaration level describes a struct, union, enum, block, argument
361  * declaration list or an abstract (unnamed) type.
362  *
363  * For nested declarations, the global 'dcs' holds all information needed for
364  * the current level, the outer levels are available via 'd_enclosing'.
365  */
366 typedef	struct decl_level {
367 	decl_level_kind d_kind;
368 	tspec_t	d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */
369 	tspec_t	d_complex_mod;	/* FLOAT or DOUBLE */
370 	tspec_t	d_sign_mod;	/* SIGNED or UNSIGN */
371 	tspec_t	d_rank_mod;	/* SHORT, LONG or LLONG */
372 	scl_t	d_scl;		/* storage class */
373 	type_t	*d_type;	/* after dcs_end_type, the pointer to the type
374 				 * used for all declarators */
375 	sym_t	*d_redeclared_symbol;
376 	unsigned int d_sou_size_in_bits;	/* size of the structure or
377 						 * union being built, without
378 						 * trailing padding */
379 	unsigned int d_sou_align_in_bits;	/* alignment of the structure
380 						 * or union being built */
381 	type_qualifiers d_qual;	/* in declaration specifiers */
382 	bool	d_inline:1;	/* inline in declaration specifiers */
383 	bool	d_multiple_storage_classes:1; /* reported in dcs_end_type */
384 	bool	d_invalid_type_combination:1;
385 	bool	d_nonempty_decl:1; /* in a function declaration, whether at
386 				 * least one tag was declared */
387 	bool	d_no_type_specifier:1;
388 	bool	d_asm:1;	/* set if d_ctx == AUTO and asm() present */
389 	bool	d_packed:1;
390 	bool	d_used:1;
391 	type_t	*d_tag_type;	/* during a member declaration, the tag type to
392 				 * which the member belongs */
393 	sym_t	*d_func_args;	/* during a function declaration, the list of
394 				 * arguments */
395 	pos_t	d_func_def_pos;	/* position of the function definition */
396 	sym_t	*d_first_dlsym;	/* first symbol declared at this level */
397 	sym_t	**d_last_dlsym;	/* points to s_level_next in the last symbol
398 				   declaration at this level */
399 	sym_t	*d_func_proto_syms; /* symbols defined in prototype */
400 	struct decl_level *d_enclosing; /* the enclosing declaration level */
401 } decl_level;
402 
403 struct parameter_list {
404 	sym_t	*first;
405 	bool	vararg:1;
406 	bool	prototype:1;
407 };
408 
409 /*
410  * A sequence of asterisks and qualifiers, from right to left.  For example,
411  * 'const ***volatile **const volatile' results in [c-v-, ----, --v-, ----,
412  * ----].  The leftmost 'const' is not included in this list, it is stored in
413  * dcs->d_qual instead.
414  */
415 typedef	struct qual_ptr {
416 	type_qualifiers qualifiers;
417 	struct	qual_ptr *p_next;
418 } qual_ptr;
419 
420 /*
421  * The values of the 'case' labels, linked via cl_next in reverse order of
422  * appearance in the code, that is from bottom to top.
423  */
424 typedef	struct case_label {
425 	val_t	cl_val;
426 	struct case_label *cl_next;
427 } case_label_t;
428 
429 typedef enum {
430 	CS_DO_WHILE,
431 	CS_FOR,
432 	CS_FUNCTION_BODY,
433 	CS_IF,
434 	CS_SWITCH,
435 	CS_WHILE
436 } control_statement_kind;
437 
438 /*
439  * Used to keep information about nested control statements.
440  */
441 typedef struct control_statement {
442 	control_statement_kind c_kind;	/* to ensure proper nesting */
443 	bool	c_loop:1;		/* 'continue' and 'break' are valid */
444 	bool	c_switch:1;		/* 'case' and 'break' are valid */
445 	bool	c_break:1;		/* the loop/switch has a reachable
446 					 * 'break' statement */
447 	bool	c_continue:1;		/* the loop has a reachable 'continue'
448 					 * statement */
449 	bool	c_default:1;		/* the switch has a 'default' label */
450 	bool	c_maybe_endless:1;	/* the controlling expression is
451 					 * always true (as in 'for (;;)' or
452 					 * 'while (1)'), there may be break
453 					 * statements though */
454 	bool	c_always_then:1;
455 	bool	c_reached_end_of_then:1;
456 	bool	c_had_return_noval:1;	/* had "return;" */
457 	bool	c_had_return_value:1;	/* had "return expr;" */
458 
459 	type_t	*c_switch_type;		/* type of switch expression */
460 	tnode_t	*c_switch_expr;
461 	case_label_t *c_case_labels;	/* list of case values */
462 
463 	memory_pool c_for_expr3_mem;	/* saved memory for end of loop
464 					 * expression in for() */
465 	tnode_t	*c_for_expr3;		/* end of loop expr in for() */
466 	pos_t	c_for_expr3_pos;	/* position of end of loop expr */
467 	pos_t	c_for_expr3_csrc_pos;	/* same for csrc_pos */
468 
469 	struct control_statement *c_surrounding;
470 } control_statement;
471 
472 typedef struct {
473 	size_t lo;			/* inclusive */
474 	size_t hi;			/* inclusive */
475 } range_t;
476 
477 typedef enum {
478 	LC_ARGSUSED,
479 	LC_BITFIELDTYPE,
480 	LC_CONSTCOND,
481 	LC_FALLTHROUGH,
482 	LC_LINTLIBRARY,
483 	LC_LINTED,
484 	LC_LONGLONG,
485 	LC_NOTREACHED,
486 	LC_PRINTFLIKE,
487 	LC_PROTOLIB,
488 	LC_SCANFLIKE,
489 	LC_VARARGS,
490 } lint_comment;
491 
492 #include "externs1.h"
493 
494 #define lint_assert(cond)						\
495 	do {								\
496 		if (!(cond))						\
497 			assert_failed(__FILE__, __LINE__, __func__, #cond); \
498 	} while (false)
499 
500 #ifdef DEBUG
501 #  include "err-msgs.h"
502 
503 /* ARGSUSED */
504 static inline void __printflike(1, 2)
check_printf(const char * fmt,...)505 check_printf(const char *fmt, ...)
506 {
507 }
508 
509 #  define wrap_check_printf_at(func, msgid, pos, args...)		\
510 	do {								\
511 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
512 		(func)(msgid, pos, ##args);				\
513 	} while (false)
514 
515 #  define error_at(msgid, pos, args...) \
516 	wrap_check_printf_at(error_at, msgid, pos, ##args)
517 #  define warning_at(msgid, pos, args...) \
518 	wrap_check_printf_at(warning_at, msgid, pos, ##args)
519 #  define message_at(msgid, pos, args...) \
520 	wrap_check_printf_at(message_at, msgid, pos, ##args)
521 
522 #  define wrap_check_printf(func, cond, msgid, args...)			\
523 	({								\
524 		if (/* CONSTCOND */cond)				\
525 			debug_step("%s:%d: %s %d '%s' in %s",		\
526 			    __FILE__, __LINE__,	#func, msgid,		\
527 			    __CONCAT(MSG_, msgid), __func__);		\
528 		check_printf(__CONCAT(MSG_, msgid), ##args);		\
529 		(func)(msgid, ##args);					\
530 		/* LINTED 129 */					\
531 	})
532 
533 #  define error(msgid, args...) wrap_check_printf(error, \
534     true, msgid, ##args)
535 #  define warning(msgid, args...) wrap_check_printf(warning, \
536     true, msgid, ##args)
537 #  define gnuism(msgid, args...) wrap_check_printf(gnuism, \
538     !allow_gcc || (!allow_trad && !allow_c99), msgid, ##args)
539 #  define c99ism(msgid, args...) wrap_check_printf(c99ism, \
540     !allow_c99 && (!allow_gcc || !allow_trad), msgid, ##args)
541 #  define c11ism(msgid, args...) wrap_check_printf(c11ism, \
542     !allow_c11 && !allow_gcc, msgid, ##args)
543 #  define c23ism(msgid, args...) wrap_check_printf(c23ism, \
544     !allow_c23, msgid, ##args)
545 #endif
546 
547 #ifdef DEBUG
548 #  define query_message(query_id, args...)				\
549 	do {								\
550 		debug_step("%s:%d: query %d '%s' in %s",		\
551 		    __FILE__, __LINE__,					\
552 		    query_id, __CONCAT(MSG_Q, query_id), __func__);	\
553 		check_printf(__CONCAT(MSG_Q, query_id), ##args);	\
554 		(query_message)(query_id, ##args);			\
555 	} while (false)
556 #else
557 #  define query_message(...)						\
558 	do {								\
559 		if (any_query_enabled)					\
560 			(query_message)(__VA_ARGS__);			\
561 	} while (false)
562 #endif
563 
564 /* Copies curr_pos, keeping things unique. */
565 static inline pos_t
unique_curr_pos(void)566 unique_curr_pos(void)
567 {
568 	pos_t curr = curr_pos;
569 	curr_pos.p_uniq++;
570 	if (curr_pos.p_file == csrc_pos.p_file)
571 		csrc_pos.p_uniq++;
572 	return curr;
573 }
574 
575 static inline bool
is_nonzero_val(const val_t * val)576 is_nonzero_val(const val_t *val)
577 {
578 	return is_floating(val->v_tspec)
579 	    ? val->u.floating != 0.0
580 	    : val->u.integer != 0;
581 }
582 
583 static inline bool
constant_is_nonzero(const tnode_t * tn)584 constant_is_nonzero(const tnode_t *tn)
585 {
586 	lint_assert(tn->tn_op == CON);
587 	lint_assert(tn->tn_type->t_tspec == tn->tn_val.v_tspec);
588 	return is_nonzero_val(&tn->tn_val);
589 }
590 
591 static inline bool
is_zero(const tnode_t * tn)592 is_zero(const tnode_t *tn)
593 {
594 	return tn != NULL && tn->tn_op == CON && !is_nonzero_val(&tn->tn_val);
595 }
596 
597 static inline bool
is_nonzero(const tnode_t * tn)598 is_nonzero(const tnode_t *tn)
599 {
600 	return tn != NULL && tn->tn_op == CON && is_nonzero_val(&tn->tn_val);
601 }
602 
603 static inline const char *
op_name(op_t op)604 op_name(op_t op)
605 {
606 	return modtab[op].m_name;
607 }
608 
609 static inline bool
is_binary(const tnode_t * tn)610 is_binary(const tnode_t *tn)
611 {
612 	return modtab[tn->tn_op].m_binary;
613 }
614 
615 static inline uint64_t
bit(unsigned i)616 bit(unsigned i)
617 {
618 	/*
619 	 * TODO: Add proper support for INT128.
620 	 * This involves changing val_t to 128 bits.
621 	 */
622 	if (i >= 64)
623 		return 0;	/* XXX: not correct for INT128 and UINT128 */
624 
625 	lint_assert(i < 64);
626 	return (uint64_t)1 << i;
627 }
628 
629 static inline bool
msb(int64_t si,tspec_t t)630 msb(int64_t si, tspec_t t)
631 {
632 	return ((uint64_t)si & bit(size_in_bits(t) - 1)) != 0;
633 }
634 
635 static inline uint64_t
value_bits(unsigned bitsize)636 value_bits(unsigned bitsize)
637 {
638 	lint_assert(bitsize > 0);
639 
640 	/* for long double (80 or 128), double _Complex (128) */
641 	/*
642 	 * XXX: double _Complex does not have 128 bits of precision,
643 	 * therefore it should never be necessary to query the value bits
644 	 * of such a type; see d_c99_complex_split.c to trigger this case.
645 	 */
646 	if (bitsize >= 64)
647 		return ~((uint64_t)0);
648 
649 	return ~(~(uint64_t)0 << bitsize);
650 }
651 
652 /* C99 6.7.8p7 */
653 static inline bool
is_struct_or_union(tspec_t t)654 is_struct_or_union(tspec_t t)
655 {
656 	return t == STRUCT || t == UNION;
657 }
658 
659 static inline bool
is_member(const sym_t * sym)660 is_member(const sym_t *sym)
661 {
662 	return sym->s_scl == STRUCT_MEMBER || sym->s_scl == UNION_MEMBER;
663 }
664 
665 static inline void
set_symtyp(symt_t symt)666 set_symtyp(symt_t symt)
667 {
668 	if (yflag)
669 		debug_step("%s: %s -> %s", __func__,
670 		    symt_name(symtyp), symt_name(symt));
671 	symtyp = symt;
672 }
673