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