tree.c revision 1.615 1 /* $NetBSD: tree.c,v 1.615 2024/03/10 09:24:54 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for
18 * The NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
37
38 #include <sys/cdefs.h>
39 #if defined(__RCSID)
40 __RCSID("$NetBSD: tree.c,v 1.615 2024/03/10 09:24:54 rillig Exp $");
41 #endif
42
43 #include <float.h>
44 #include <limits.h>
45 #include <math.h>
46 #include <signal.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 #include "lint1.h"
51
52
53 typedef struct integer_constraints {
54 int64_t smin; /* signed minimum */
55 int64_t smax; /* signed maximum */
56 uint64_t umin; /* unsigned minimum */
57 uint64_t umax; /* unsigned maximum */
58 uint64_t bset; /* bits that are definitely set */
59 uint64_t bclr; /* bits that are definitely clear */
60 } integer_constraints;
61
62
63 static uint64_t
64 u64_fill_right(uint64_t x)
65 {
66 x |= x >> 1;
67 x |= x >> 2;
68 x |= x >> 4;
69 x |= x >> 8;
70 x |= x >> 16;
71 x |= x >> 32;
72 return x;
73 }
74
75 static bool
76 str_ends_with(const char *haystack, const char *needle)
77 {
78 size_t hlen = strlen(haystack);
79 size_t nlen = strlen(needle);
80
81 return nlen <= hlen &&
82 memcmp(haystack + hlen - nlen, needle, nlen) == 0;
83 }
84
85 static unsigned
86 width_in_bits(const type_t *tp)
87 {
88
89 lint_assert(is_integer(tp->t_tspec));
90 return tp->t_bitfield
91 ? tp->t_bit_field_width
92 : size_in_bits(tp->t_tspec);
93 }
94
95 static int
96 portable_rank_cmp(tspec_t t1, tspec_t t2)
97 {
98 const ttab_t *p1 = type_properties(t1), *p2 = type_properties(t2);
99 lint_assert(p1->tt_rank_kind == p2->tt_rank_kind);
100 lint_assert(p1->tt_rank_value > 0);
101 lint_assert(p2->tt_rank_value > 0);
102 return (int)p1->tt_rank_value - (int)p2->tt_rank_value;
103 }
104
105 static bool
106 ic_maybe_signed(const type_t *tp, const integer_constraints *ic)
107 {
108 return !is_uinteger(tp->t_tspec) && ic->bclr >> 63 == 0;
109 }
110
111 static integer_constraints
112 ic_any(const type_t *tp)
113 {
114 integer_constraints c;
115
116 uint64_t vbits = value_bits(width_in_bits(tp));
117 if (is_uinteger(tp->t_tspec)) {
118 c.smin = INT64_MIN;
119 c.smax = INT64_MAX;
120 c.umin = 0;
121 c.umax = vbits;
122 c.bset = 0;
123 c.bclr = ~c.umax;
124 } else {
125 c.smin = (int64_t)-1 - (int64_t)(vbits >> 1);
126 c.smax = (int64_t)(vbits >> 1);
127 c.umin = 0;
128 c.umax = UINT64_MAX;
129 c.bset = 0;
130 c.bclr = 0;
131 }
132 return c;
133 }
134
135 static integer_constraints
136 ic_con(const type_t *tp, const val_t *v)
137 {
138 integer_constraints c;
139
140 lint_assert(is_integer(tp->t_tspec));
141 int64_t si = v->u.integer;
142 uint64_t ui = (uint64_t)si;
143 c.smin = si;
144 c.smax = si;
145 c.umin = ui;
146 c.umax = ui;
147 c.bset = ui;
148 c.bclr = ~ui;
149 return c;
150 }
151
152 static integer_constraints
153 ic_cvt(const type_t *ntp, const type_t *otp, integer_constraints a)
154 {
155 unsigned nw = width_in_bits(ntp);
156 unsigned ow = width_in_bits(otp);
157 bool nu = is_uinteger(ntp->t_tspec);
158 bool ou = is_uinteger(otp->t_tspec);
159
160 if (nw >= ow && nu == ou)
161 return a;
162 if (nw > ow && ou)
163 return a;
164 return ic_any(ntp);
165 }
166
167 static integer_constraints
168 ic_bitand(integer_constraints a, integer_constraints b)
169 {
170 integer_constraints c;
171
172 c.smin = INT64_MIN;
173 c.smax = INT64_MAX;
174 c.umin = 0;
175 c.umax = UINT64_MAX;
176 c.bset = a.bset & b.bset;
177 c.bclr = a.bclr | b.bclr;
178 return c;
179 }
180
181 static integer_constraints
182 ic_bitor(integer_constraints a, integer_constraints b)
183 {
184 integer_constraints c;
185
186 c.smin = INT64_MIN;
187 c.smax = INT64_MAX;
188 c.umin = 0;
189 c.umax = UINT64_MAX;
190 c.bset = a.bset | b.bset;
191 c.bclr = a.bclr & b.bclr;
192 return c;
193 }
194
195 static integer_constraints
196 ic_mod(const type_t *tp, integer_constraints a, integer_constraints b)
197 {
198 integer_constraints c;
199
200 if (ic_maybe_signed(tp, &a) || ic_maybe_signed(tp, &b))
201 return ic_any(tp);
202
203 c.smin = INT64_MIN;
204 c.smax = INT64_MAX;
205 c.umin = 0;
206 c.umax = b.umax - 1;
207 c.bset = 0;
208 c.bclr = ~u64_fill_right(c.umax);
209 return c;
210 }
211
212 static integer_constraints
213 ic_shl(const type_t *tp, integer_constraints a, integer_constraints b)
214 {
215 integer_constraints c;
216 unsigned int amount;
217
218 if (ic_maybe_signed(tp, &a))
219 return ic_any(tp);
220
221 if (b.smin == b.smax && b.smin >= 0 && b.smin < 64)
222 amount = (unsigned int)b.smin;
223 else if (b.umin == b.umax && b.umin < 64)
224 amount = (unsigned int)b.umin;
225 else
226 return ic_any(tp);
227
228 c.smin = INT64_MIN;
229 c.smax = INT64_MAX;
230 c.umin = 0;
231 c.umax = UINT64_MAX;
232 c.bset = a.bset << amount;
233 c.bclr = a.bclr << amount | (((uint64_t)1 << amount) - 1);
234 return c;
235 }
236
237 static integer_constraints
238 ic_shr(const type_t *tp, integer_constraints a, integer_constraints b)
239 {
240 integer_constraints c;
241 unsigned int amount;
242
243 if (ic_maybe_signed(tp, &a))
244 return ic_any(tp);
245
246 if (b.smin == b.smax && b.smin >= 0 && b.smin < 64)
247 amount = (unsigned int)b.smin;
248 else if (b.umin == b.umax && b.umin < 64)
249 amount = (unsigned int)b.umin;
250 else
251 return ic_any(tp);
252
253 c.smin = INT64_MIN;
254 c.smax = INT64_MAX;
255 c.umin = 0;
256 c.umax = UINT64_MAX;
257 c.bset = a.bset >> amount;
258 c.bclr = a.bclr >> amount | ~(~(uint64_t)0 >> amount);
259 return c;
260 }
261
262 static integer_constraints
263 ic_cond(integer_constraints a, integer_constraints b)
264 {
265 integer_constraints c;
266
267 c.smin = a.smin < b.smin ? a.smin : b.smin;
268 c.smax = a.smax > b.smax ? a.smax : b.smax;
269 c.umin = a.umin < b.umin ? a.umin : b.umin;
270 c.umax = a.umax > b.umax ? a.umax : b.umax;
271 c.bset = a.bset | b.bset;
272 c.bclr = a.bclr & b.bclr;
273 return c;
274 }
275
276 static integer_constraints
277 ic_expr(const tnode_t *tn)
278 {
279 integer_constraints lc, rc;
280
281 lint_assert(is_integer(tn->tn_type->t_tspec));
282
283 switch (tn->tn_op) {
284 case CON:
285 return ic_con(tn->tn_type, &tn->u.value);
286 case CVT:
287 if (!is_integer(tn->u.ops.left->tn_type->t_tspec))
288 return ic_any(tn->tn_type);
289 lc = ic_expr(tn->u.ops.left);
290 return ic_cvt(tn->tn_type, tn->u.ops.left->tn_type, lc);
291 case MOD:
292 lc = ic_expr(before_conversion(tn->u.ops.left));
293 rc = ic_expr(before_conversion(tn->u.ops.right));
294 return ic_mod(tn->tn_type, lc, rc);
295 case SHL:
296 lc = ic_expr(tn->u.ops.left);
297 rc = ic_expr(tn->u.ops.right);
298 return ic_shl(tn->tn_type, lc, rc);
299 case SHR:
300 lc = ic_expr(tn->u.ops.left);
301 rc = ic_expr(tn->u.ops.right);
302 return ic_shr(tn->tn_type, lc, rc);
303 case BITAND:
304 lc = ic_expr(tn->u.ops.left);
305 rc = ic_expr(tn->u.ops.right);
306 return ic_bitand(lc, rc);
307 case BITOR:
308 lc = ic_expr(tn->u.ops.left);
309 rc = ic_expr(tn->u.ops.right);
310 return ic_bitor(lc, rc);
311 case QUEST:
312 lc = ic_expr(tn->u.ops.right->u.ops.left);
313 rc = ic_expr(tn->u.ops.right->u.ops.right);
314 return ic_cond(lc, rc);
315 default:
316 return ic_any(tn->tn_type);
317 }
318 }
319
320 uint64_t
321 possible_bits(const tnode_t *tn)
322 {
323 return ~ic_expr(tn).bclr;
324 }
325
326 /* Build 'pointer to tp', 'array of tp' or 'function returning tp'. */
327 type_t *
328 block_derive_type(type_t *tp, tspec_t t)
329 {
330
331 type_t *tp2 = block_zero_alloc(sizeof(*tp2), "type");
332 tp2->t_tspec = t;
333 tp2->t_subt = tp;
334 return tp2;
335 }
336
337 /*
338 * Derive 'pointer to tp' or 'function returning tp'.
339 * The memory is freed at the end of the current expression.
340 */
341 type_t *
342 expr_derive_type(type_t *tp, tspec_t t)
343 {
344
345 type_t *tp2 = expr_zero_alloc(sizeof(*tp2), "type");
346 tp2->t_tspec = t;
347 tp2->t_subt = tp;
348 return tp2;
349 }
350
351 /* Create an expression from a unary or binary operator and its operands. */
352 static tnode_t *
353 build_op(op_t op, bool sys, type_t *type, tnode_t *ln, tnode_t *rn)
354 {
355
356 tnode_t *ntn = expr_alloc_tnode();
357 ntn->tn_op = op;
358 ntn->tn_type = type;
359 ntn->tn_sys = sys;
360 ntn->u.ops.left = ln;
361 ntn->u.ops.right = rn;
362
363 if (op == INDIR || op == FSEL) {
364 lint_assert(ln->tn_type->t_tspec == PTR);
365 tspec_t t = ln->tn_type->t_subt->t_tspec;
366 ntn->tn_lvalue = t != FUNC && t != VOID;
367 }
368
369 return ntn;
370 }
371
372 tnode_t *
373 build_constant(type_t *tp, val_t *v)
374 {
375
376 tnode_t *n = expr_alloc_tnode();
377 n->tn_op = CON;
378 n->tn_type = tp;
379 n->u.value = *v;
380 n->u.value.v_tspec = tp->t_tspec;
381 free(v);
382 return n;
383 }
384
385 static tnode_t *
386 build_integer_constant(tspec_t t, int64_t si)
387 {
388
389 tnode_t *n = expr_alloc_tnode();
390 n->tn_op = CON;
391 n->tn_type = gettyp(t);
392 n->u.value.v_tspec = t;
393 n->u.value.v_unsigned_since_c90 = false;
394 n->u.value.v_char_constant = false;
395 n->u.value.u.integer = si;
396 return n;
397 }
398
399 static void
400 fallback_symbol(sym_t *sym)
401 {
402
403 if (Tflag && fallback_symbol_strict_bool(sym))
404 return;
405
406 if (block_level > 0 && (strcmp(sym->s_name, "__FUNCTION__") == 0 ||
407 strcmp(sym->s_name, "__PRETTY_FUNCTION__") == 0)) {
408 /* __FUNCTION__/__PRETTY_FUNCTION__ is a GCC extension */
409 gnuism(316);
410 // XXX: Should probably be ARRAY instead of PTR.
411 sym->s_type = block_derive_type(gettyp(CHAR), PTR);
412 sym->s_type->t_const = true;
413 return;
414 }
415
416 if (funcsym != NULL && strcmp(sym->s_name, "__func__") == 0) {
417 if (!allow_c99)
418 /* __func__ is a C99 feature */
419 warning(317);
420 /* C11 6.4.2.2 */
421 sym->s_type = block_derive_type(gettyp(CHAR), ARRAY);
422 sym->s_type->t_const = true;
423 sym->s_type->u.dimension = (int)strlen(funcsym->s_name) + 1;
424 return;
425 }
426
427 /* '%s' undefined */
428 error(99, sym->s_name);
429 }
430
431 /*
432 * Functions that are predeclared by GCC or other compilers can be called
433 * with arbitrary arguments. Since lint usually runs after a successful
434 * compilation, it's the compiler's job to catch any errors.
435 */
436 bool
437 is_compiler_builtin(const char *name)
438 {
439 /* https://gcc.gnu.org/onlinedocs/gcc/C-Extensions.html */
440 if (allow_gcc) {
441 if (strncmp(name, "__atomic_", 9) == 0 ||
442 strncmp(name, "__builtin_", 10) == 0 ||
443 strcmp(name, "alloca") == 0 ||
444 /* obsolete but still in use, as of 2021 */
445 strncmp(name, "__sync_", 7) == 0)
446 return true;
447 }
448
449 /* https://software.intel.com/sites/landingpage/IntrinsicsGuide/ */
450 if (strncmp(name, "_mm_", 4) == 0)
451 return true;
452
453 return false;
454 }
455
456 /* https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html */
457 static bool
458 is_gcc_bool_builtin(const char *name)
459 {
460 return strncmp(name, "__builtin_", 10) == 0 &&
461 (str_ends_with(name, "_overflow") ||
462 str_ends_with(name, "_overflow_p"));
463 }
464
465 static void
466 build_name_call(sym_t *sym)
467 {
468
469 if (is_compiler_builtin(sym->s_name)) {
470 /*
471 * Do not warn about these, just assume that they are regular
472 * functions compatible with non-prototype calling conventions.
473 */
474 if (allow_gcc && is_gcc_bool_builtin(sym->s_name))
475 sym->s_type = gettyp(BOOL);
476 } else if (allow_c99)
477 /* function '%s' implicitly declared to return int */
478 error(215, sym->s_name);
479 else if (!allow_trad)
480 /* function '%s' implicitly declared to return int */
481 warning(215, sym->s_name);
482
483 /* XXX if !allow_c90, the symbol should be exported to level 0 */
484 sym->s_type = block_derive_type(sym->s_type, FUNC);
485 }
486
487 /* Create a node for a name (symbol table entry). */
488 tnode_t *
489 build_name(sym_t *sym, bool is_funcname)
490 {
491
492 if (sym->s_scl == NO_SCL && !in_gcc_attribute) {
493 sym->s_scl = EXTERN;
494 sym->s_def = DECL;
495 if (is_funcname)
496 build_name_call(sym);
497 else
498 fallback_symbol(sym);
499 }
500
501 lint_assert(sym->s_kind == SK_VCFT || sym->s_kind == SK_MEMBER);
502
503 tnode_t *n = expr_alloc_tnode();
504 n->tn_type = sym->s_type;
505 if (sym->s_scl == BOOL_CONST) {
506 n->tn_op = CON;
507 n->u.value.v_tspec = BOOL;
508 n->u.value.v_unsigned_since_c90 = false;
509 n->u.value.v_char_constant = false;
510 n->u.value.u.integer = sym->u.s_bool_constant ? 1 : 0;
511 } else if (sym->s_scl == ENUM_CONST) {
512 n->tn_op = CON;
513 n->u.value.v_tspec = INT; /* ENUM is in n->tn_type */
514 n->u.value.v_unsigned_since_c90 = false;
515 n->u.value.v_char_constant = false;
516 n->u.value.u.integer = sym->u.s_enum_constant;
517 } else {
518 n->tn_op = NAME;
519 n->u.sym = sym;
520 if (sym->s_kind == SK_VCFT && sym->s_type->t_tspec != FUNC)
521 n->tn_lvalue = true;
522 }
523
524 return n;
525 }
526
527 tnode_t *
528 build_string(buffer *lit)
529 {
530 size_t value_len = lit->len;
531 if (lit->data != NULL) {
532 quoted_iterator it = { .end = 0 };
533 for (value_len = 0; quoted_next(lit, &it); value_len++)
534 continue;
535 }
536
537 type_t *tp = expr_zero_alloc(sizeof(*tp), "type");
538 tp->t_tspec = ARRAY;
539 tp->t_subt = gettyp(lit->data != NULL ? CHAR : WCHAR_TSPEC);
540 tp->u.dimension = (int)(value_len + 1);
541
542 tnode_t *n = expr_alloc_tnode();
543 n->tn_op = STRING;
544 n->tn_type = tp;
545 n->tn_lvalue = true;
546
547 n->u.str_literals = expr_zero_alloc(sizeof(*n->u.str_literals), "tnode.string");
548 n->u.str_literals->len = lit->len;
549
550 if (lit->data != NULL) {
551 n->u.str_literals->data = expr_zero_alloc(lit->len + 1,
552 "tnode.string.data");
553 (void)memcpy(n->u.str_literals->data, lit->data, lit->len + 1);
554 free(lit->data);
555 }
556 free(lit);
557
558 return n;
559 }
560
561 tnode_t *
562 build_generic_selection(const tnode_t *expr,
563 struct generic_association *sel)
564 {
565 tnode_t *default_result = NULL;
566
567 for (; sel != NULL; sel = sel->ga_prev) {
568 if (expr != NULL &&
569 types_compatible(sel->ga_arg, expr->tn_type,
570 false, false, NULL))
571 return sel->ga_result;
572 else if (sel->ga_arg == NULL)
573 default_result = sel->ga_result;
574 }
575 return default_result;
576 }
577
578 static bool
579 is_out_of_char_range(const tnode_t *tn)
580 {
581 return tn->tn_op == CON &&
582 !tn->u.value.v_char_constant &&
583 !(0 <= tn->u.value.u.integer &&
584 tn->u.value.u.integer < 1 << (CHAR_SIZE - 1));
585 }
586
587 static bool
588 check_nonportable_char_comparison(op_t op,
589 const tnode_t *ln, tspec_t lt,
590 const tnode_t *rn, tspec_t rt)
591 {
592 if (!(hflag || pflag))
593 return true;
594
595 if (lt == CHAR && is_out_of_char_range(rn)) {
596 char buf[128];
597 (void)snprintf(buf, sizeof(buf), "%s %d",
598 op_name(op), (int)rn->u.value.u.integer);
599 /* nonportable character comparison '%s' */
600 warning(230, buf);
601 return false;
602 }
603 if (rt == CHAR && is_out_of_char_range(ln)) {
604 char buf[128];
605 (void)snprintf(buf, sizeof(buf), "%d %s ?",
606 (int)ln->u.value.u.integer, op_name(op));
607 /* nonportable character comparison '%s' */
608 warning(230, buf);
609 return false;
610 }
611 return true;
612 }
613
614 static void
615 check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
616 {
617
618 tspec_t lt = ln->tn_type->t_tspec;
619 tspec_t rt = rn->tn_type->t_tspec;
620
621 if (ln->tn_op != CON && rn->tn_op != CON)
622 return;
623
624 if (!is_integer(lt) || !is_integer(rt))
625 return;
626
627 if (any_query_enabled && !in_system_header) {
628 if (lt == CHAR && rn->tn_op == CON &&
629 !rn->u.value.v_char_constant) {
630 /* comparison '%s' of 'char' with plain integer %d */
631 query_message(14,
632 op_name(op), (int)rn->u.value.u.integer);
633 }
634 if (rt == CHAR && ln->tn_op == CON &&
635 !ln->u.value.v_char_constant) {
636 /* comparison '%s' of 'char' with plain integer %d */
637 query_message(14,
638 op_name(op), (int)ln->u.value.u.integer);
639 }
640 }
641
642 if (!check_nonportable_char_comparison(op, ln, lt, rn, rt))
643 return;
644
645 if (is_uinteger(lt) && !is_uinteger(rt) &&
646 rn->tn_op == CON && rn->u.value.u.integer <= 0) {
647 if (rn->u.value.u.integer < 0) {
648 /* operator '%s' compares '%s' with '%s' */
649 warning(162, op_name(op),
650 type_name(ln->tn_type), "negative constant");
651 } else if (op == LT || op == GE)
652 /* operator '%s' compares '%s' with '%s' */
653 warning(162, op_name(op), type_name(ln->tn_type), "0");
654 return;
655 }
656 if (is_uinteger(rt) && !is_uinteger(lt) &&
657 ln->tn_op == CON && ln->u.value.u.integer <= 0) {
658 if (ln->u.value.u.integer < 0) {
659 /* operator '%s' compares '%s' with '%s' */
660 warning(162, op_name(op),
661 "negative constant", type_name(rn->tn_type));
662 } else if (op == GT || op == LE)
663 /* operator '%s' compares '%s' with '%s' */
664 warning(162, op_name(op), "0", type_name(rn->tn_type));
665 return;
666 }
667 }
668
669 static const tspec_t arith_rank[] = {
670 LDOUBLE, DOUBLE, FLOAT,
671 #ifdef INT128_SIZE
672 UINT128, INT128,
673 #endif
674 ULLONG, LLONG,
675 ULONG, LONG,
676 UINT, INT,
677 };
678
679 /* Keep unsigned in traditional C */
680 static tspec_t
681 usual_arithmetic_conversion_trad(tspec_t lt, tspec_t rt)
682 {
683
684 size_t i;
685 for (i = 0; arith_rank[i] != INT; i++)
686 if (lt == arith_rank[i] || rt == arith_rank[i])
687 break;
688
689 tspec_t t = arith_rank[i];
690 if (is_uinteger(lt) || is_uinteger(rt))
691 if (is_integer(t) && !is_uinteger(t))
692 return unsigned_type(t);
693 return t;
694 }
695
696 static tspec_t
697 usual_arithmetic_conversion_c90(tspec_t lt, tspec_t rt)
698 {
699
700 if (lt == rt)
701 return lt;
702
703 if (lt == LCOMPLEX || rt == LCOMPLEX)
704 return LCOMPLEX;
705 if (lt == DCOMPLEX || rt == DCOMPLEX)
706 return DCOMPLEX;
707 if (lt == FCOMPLEX || rt == FCOMPLEX)
708 return FCOMPLEX;
709 if (lt == LDOUBLE || rt == LDOUBLE)
710 return LDOUBLE;
711 if (lt == DOUBLE || rt == DOUBLE)
712 return DOUBLE;
713 if (lt == FLOAT || rt == FLOAT)
714 return FLOAT;
715
716 if (size_in_bits(lt) > size_in_bits(rt))
717 return lt;
718 if (size_in_bits(lt) < size_in_bits(rt))
719 return rt;
720
721 size_t i;
722 for (i = 3; arith_rank[i] != INT; i++)
723 if (arith_rank[i] == lt || arith_rank[i] == rt)
724 break;
725 if ((is_uinteger(lt) || is_uinteger(rt)) &&
726 !is_uinteger(arith_rank[i]))
727 i--;
728 return arith_rank[i];
729 }
730
731 static tnode_t *
732 apply_usual_arithmetic_conversions(op_t op, tnode_t *tn, tspec_t t)
733 {
734 type_t *ntp = expr_dup_type(tn->tn_type);
735 ntp->t_tspec = t;
736 if (tn->tn_op != CON) {
737 /* usual arithmetic conversion for '%s' from '%s' to '%s' */
738 query_message(4, op_name(op),
739 type_name(tn->tn_type), type_name(ntp));
740 }
741 return convert(op, 0, ntp, tn);
742 }
743
744 /*
745 * Apply the "usual arithmetic conversions" (C99 6.3.1.8), which gives both
746 * operands the same type.
747 */
748 static void
749 balance(op_t op, tnode_t **lnp, tnode_t **rnp)
750 {
751
752 tspec_t lt = (*lnp)->tn_type->t_tspec;
753 tspec_t rt = (*rnp)->tn_type->t_tspec;
754 if (!is_arithmetic(lt) || !is_arithmetic(rt))
755 return;
756
757 tspec_t t = allow_c90
758 ? usual_arithmetic_conversion_c90(lt, rt)
759 : usual_arithmetic_conversion_trad(lt, rt);
760
761 if (t != lt)
762 *lnp = apply_usual_arithmetic_conversions(op, *lnp, t);
763 if (t != rt)
764 *rnp = apply_usual_arithmetic_conversions(op, *rnp, t);
765 }
766
767 static tnode_t *
768 build_address(bool sys, tnode_t *tn, bool force)
769 {
770 tspec_t t;
771
772 if (!force && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
773 if (!allow_c90)
774 /* '&' before array or function: ignored */
775 warning(127);
776 return tn;
777 }
778
779 /* eliminate '&*' */
780 if (tn->tn_op == INDIR &&
781 tn->u.ops.left->tn_type->t_tspec == PTR &&
782 tn->u.ops.left->tn_type->t_subt == tn->tn_type) {
783 return tn->u.ops.left;
784 }
785
786 return build_op(ADDR, sys, expr_derive_type(tn->tn_type, PTR),
787 tn, NULL);
788 }
789
790 /*
791 * XXX
792 * Note: There appear to be a number of bugs in detecting overflow in
793 * this function. An audit and a set of proper regression tests are needed.
794 * --Perry Metzger, Nov. 16, 2001
795 */
796 static tnode_t *
797 fold_constant_integer(tnode_t *tn)
798 {
799
800 val_t *v = xcalloc(1, sizeof(*v));
801 v->v_tspec = tn->tn_type->t_tspec;
802
803 lint_assert(has_operands(tn));
804 tspec_t t = tn->u.ops.left->tn_type->t_tspec;
805 bool utyp = !is_integer(t) || is_uinteger(t);
806 int64_t sl = tn->u.ops.left->u.value.u.integer, sr = 0;
807 uint64_t ul = sl, ur = 0;
808 if (is_binary(tn))
809 ur = sr = tn->u.ops.right->u.value.u.integer;
810
811 uint64_t mask = value_bits(size_in_bits(t));
812 int64_t max_value = (int64_t)(mask >> 1);
813 int64_t min_value = -max_value - 1;
814 bool ovfl = false;
815
816 int64_t si;
817 switch (tn->tn_op) {
818 case COMPL:
819 si = ~sl;
820 break;
821 case UPLUS:
822 si = sl;
823 break;
824 case UMINUS:
825 si = sl == INT64_MIN ? sl : -sl;
826 if (sl != 0 && msb(si, t) == msb(sl, t))
827 ovfl = true;
828 break;
829 case MULT:
830 if (utyp) {
831 si = (int64_t)(ul * ur);
832 if (si != (si & (int64_t)mask))
833 ovfl = true;
834 else if (ul != 0 && si / ul != ur)
835 ovfl = true;
836 } else {
837 si = sl * sr;
838 if (msb(si, t) != (msb(sl, t) ^ msb(sr, t)))
839 ovfl = true;
840 }
841 break;
842 case DIV:
843 if (sr == 0) {
844 /* division by 0 */
845 error(139);
846 si = utyp ? -1 : max_value;
847 } else if (!utyp && sl == min_value && sr == -1) {
848 ovfl = true;
849 si = sl;
850 } else
851 si = utyp ? (int64_t)(ul / ur) : sl / sr;
852 break;
853 case MOD:
854 if (sr == 0) {
855 /* modulus by 0 */
856 error(140);
857 si = 0;
858 } else
859 si = utyp ? (int64_t)(ul % ur) : sl % sr;
860 break;
861 case PLUS:
862 si = utyp ? (int64_t)(ul + ur) : sl + sr;
863 if (msb(sl, t) && msb(sr, t) && !msb(si, t))
864 ovfl = true;
865 if (!utyp && !msb(sl, t) && !msb(sr, t) && msb(si, t))
866 ovfl = true;
867 break;
868 case MINUS:
869 si = utyp ? (int64_t)(ul - ur) : sl - sr;
870 if (!utyp && msb(sl, t) && !msb(sr, t) && !msb(si, t))
871 ovfl = true;
872 if (!msb(sl, t) && msb(sr, t) && msb(si, t))
873 ovfl = true;
874 break;
875 case SHL:
876 /* TODO: warn about out-of-bounds 'sr'. */
877 /* TODO: warn about overflow in signed '<<'. */
878 si = utyp ? (int64_t)(ul << (sr & 63)) : sl << (sr & 63);
879 break;
880 case SHR:
881 /*
882 * The sign must be explicitly extended because shifts of
883 * signed values are implementation dependent.
884 */
885 /* TODO: warn about out-of-bounds 'sr'. */
886 si = (int64_t)(ul >> (sr & 63));
887 si = convert_integer(si, t, size_in_bits(t) - (int)sr);
888 break;
889 case LT:
890 si = (utyp ? ul < ur : sl < sr) ? 1 : 0;
891 break;
892 case LE:
893 si = (utyp ? ul <= ur : sl <= sr) ? 1 : 0;
894 break;
895 case GT:
896 si = (utyp ? ul > ur : sl > sr) ? 1 : 0;
897 break;
898 case GE:
899 si = (utyp ? ul >= ur : sl >= sr) ? 1 : 0;
900 break;
901 case EQ:
902 si = (utyp ? ul == ur : sl == sr) ? 1 : 0;
903 break;
904 case NE:
905 si = (utyp ? ul != ur : sl != sr) ? 1 : 0;
906 break;
907 case BITAND:
908 si = utyp ? (int64_t)(ul & ur) : sl & sr;
909 break;
910 case BITXOR:
911 si = utyp ? (int64_t)(ul ^ ur) : sl ^ sr;
912 break;
913 case BITOR:
914 si = utyp ? (int64_t)(ul | ur) : sl | sr;
915 break;
916 default:
917 lint_assert(/*CONSTCOND*/false);
918 }
919
920 /* XXX: The overflow check does not work for 64-bit integers. */
921 if (ovfl ||
922 ((si | mask) != ~(uint64_t)0 && (si & ~mask) != 0)) {
923 if (hflag)
924 /* operator '%s' produces integer overflow */
925 warning(141, op_name(tn->tn_op));
926 }
927
928 v->u.integer = convert_integer(si, t, 0);
929
930 tnode_t *cn = build_constant(tn->tn_type, v);
931 if (tn->u.ops.left->tn_system_dependent)
932 cn->tn_system_dependent = true;
933 if (is_binary(tn) && tn->u.ops.right->tn_system_dependent)
934 cn->tn_system_dependent = true;
935
936 return cn;
937 }
938
939 static tnode_t *
940 build_struct_access(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
941 {
942
943 lint_assert(rn->tn_op == NAME);
944 lint_assert(is_member(rn->u.sym));
945
946 bool lvalue = op == ARROW || ln->tn_lvalue;
947
948 if (op == POINT)
949 ln = build_address(sys, ln, true);
950 else if (ln->tn_type->t_tspec != PTR) {
951 lint_assert(!allow_c90);
952 lint_assert(is_integer(ln->tn_type->t_tspec));
953 ln = convert(NOOP, 0, expr_derive_type(gettyp(VOID), PTR), ln);
954 }
955
956 tnode_t *ctn = build_integer_constant(PTRDIFF_TSPEC,
957 rn->u.sym->u.s_member.sm_offset_in_bits / CHAR_SIZE);
958
959 type_t *ptr_tp = expr_derive_type(rn->tn_type, PTR);
960 tnode_t *ntn = build_op(PLUS, sys, ptr_tp, ln, ctn);
961 if (ln->tn_op == CON)
962 ntn = fold_constant_integer(ntn);
963
964 op_t nop = rn->tn_type->t_bitfield ? FSEL : INDIR;
965 ntn = build_op(nop, sys, ntn->tn_type->t_subt, ntn, NULL);
966 if (!lvalue)
967 ntn->tn_lvalue = false;
968
969 return ntn;
970 }
971
972 /*
973 * Get the size in bytes of type tp->t_subt, as a constant expression of type
974 * ptrdiff_t as seen from the target platform.
975 */
976 static tnode_t *
977 subt_size_in_bytes(type_t *tp)
978 {
979
980 lint_assert(tp->t_tspec == PTR);
981 tp = tp->t_subt;
982
983 int elem = 1;
984 while (tp->t_tspec == ARRAY) {
985 elem *= tp->u.dimension;
986 tp = tp->t_subt;
987 }
988
989 int elsz_in_bits = 0;
990 switch (tp->t_tspec) {
991 case FUNC:
992 /* pointer to function is not allowed here */
993 error(110);
994 break;
995 case VOID:
996 /* cannot do pointer arithmetic on operand of unknown size */
997 gnuism(136);
998 break;
999 case STRUCT:
1000 case UNION:
1001 if ((elsz_in_bits = (int)tp->u.sou->sou_size_in_bits) == 0)
1002 /* cannot do pointer arithmetic on operand of ... */
1003 error(136);
1004 break;
1005 case ENUM:
1006 if (is_incomplete(tp))
1007 /* cannot do pointer arithmetic on operand of ... */
1008 warning(136);
1009 /* FALLTHROUGH */
1010 default:
1011 if ((elsz_in_bits = size_in_bits(tp->t_tspec)) == 0)
1012 /* cannot do pointer arithmetic on operand of ... */
1013 error(136);
1014 else
1015 lint_assert(elsz_in_bits != -1);
1016 break;
1017 }
1018
1019 if (elem == 0 && elsz_in_bits != 0)
1020 /* cannot do pointer arithmetic on operand of unknown size */
1021 error(136);
1022
1023 if (elsz_in_bits == 0)
1024 elsz_in_bits = CHAR_SIZE;
1025
1026 return build_integer_constant(PTRDIFF_TSPEC,
1027 (int64_t)(elem * elsz_in_bits / CHAR_SIZE));
1028 }
1029
1030 static tnode_t *
1031 build_prepost_incdec(op_t op, bool sys, tnode_t *ln)
1032 {
1033
1034 lint_assert(ln != NULL);
1035 tnode_t *cn = ln->tn_type->t_tspec == PTR
1036 ? subt_size_in_bytes(ln->tn_type)
1037 : build_integer_constant(INT, 1);
1038 return build_op(op, sys, ln->tn_type, ln, cn);
1039 }
1040
1041 static void
1042 check_enum_array_index(const tnode_t *ln, const tnode_t *rn)
1043 {
1044
1045 if (ln->tn_op != ADDR || ln->u.ops.left->tn_op != NAME)
1046 return;
1047
1048 const type_t *ltp = ln->u.ops.left->tn_type;
1049 if (ltp->t_tspec != ARRAY || ltp->t_incomplete_array)
1050 return;
1051
1052 if (rn->tn_op != CVT || !rn->tn_type->t_is_enum)
1053 return;
1054 if (rn->u.ops.left->tn_op != LOAD)
1055 return;
1056
1057 const type_t *rtp = rn->u.ops.left->tn_type;
1058 const sym_t *ec = rtp->u.enumer->en_first_enumerator;
1059 const sym_t *max_ec = ec;
1060 lint_assert(ec != NULL);
1061 for (ec = ec->s_next; ec != NULL; ec = ec->s_next)
1062 if (ec->u.s_enum_constant > max_ec->u.s_enum_constant)
1063 max_ec = ec;
1064
1065 int64_t max_enum_value = max_ec->u.s_enum_constant;
1066 lint_assert(INT_MIN <= max_enum_value && max_enum_value <= INT_MAX);
1067
1068 int max_array_index = ltp->u.dimension - 1;
1069 if (max_enum_value == max_array_index)
1070 return;
1071
1072 if (max_enum_value == max_array_index + 1 &&
1073 (strstr(max_ec->s_name, "MAX") != NULL ||
1074 strstr(max_ec->s_name, "max") != NULL ||
1075 strstr(max_ec->s_name, "NUM") != NULL ||
1076 strstr(max_ec->s_name, "num") != NULL))
1077 return;
1078
1079 /* maximum value %d of '%s' does not match maximum array index %d */
1080 warning(348, (int)max_enum_value, type_name(rtp), max_array_index);
1081 print_previous_declaration(max_ec);
1082 }
1083
1084 static tnode_t *
1085 build_plus_minus(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
1086 {
1087
1088 if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) {
1089 tnode_t *tmp = ln;
1090 ln = rn;
1091 rn = tmp;
1092 /* pointer addition has integer on the left-hand side */
1093 query_message(5);
1094 }
1095
1096 /* pointer +- integer */
1097 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
1098 lint_assert(is_integer(rn->tn_type->t_tspec));
1099
1100 check_ctype_macro_invocation(ln, rn);
1101 check_enum_array_index(ln, rn);
1102
1103 tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
1104 if (rn->tn_type->t_tspec != elsz->tn_type->t_tspec)
1105 rn = convert(NOOP, 0, elsz->tn_type, rn);
1106
1107 tnode_t *prod = build_op(MULT, sys, rn->tn_type, rn, elsz);
1108 if (rn->tn_op == CON)
1109 prod = fold_constant_integer(prod);
1110
1111 return build_op(op, sys, ln->tn_type, ln, prod);
1112 }
1113
1114 /* pointer - pointer */
1115 if (rn->tn_type->t_tspec == PTR) {
1116 lint_assert(ln->tn_type->t_tspec == PTR);
1117 lint_assert(op == MINUS);
1118
1119 type_t *ptrdiff = gettyp(PTRDIFF_TSPEC);
1120 tnode_t *raw_diff = build_op(op, sys, ptrdiff, ln, rn);
1121 if (ln->tn_op == CON && rn->tn_op == CON)
1122 raw_diff = fold_constant_integer(raw_diff);
1123
1124 tnode_t *elsz = subt_size_in_bytes(ln->tn_type);
1125 balance(NOOP, &raw_diff, &elsz);
1126
1127 return build_op(DIV, sys, ptrdiff, raw_diff, elsz);
1128 }
1129
1130 return build_op(op, sys, ln->tn_type, ln, rn);
1131 }
1132
1133 static tnode_t *
1134 build_bit_shift(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
1135 {
1136
1137 if (!allow_c90 && rn->tn_type->t_tspec != INT)
1138 // XXX: C1978 7.5 says: "Both [operators] perform the usual
1139 // arithmetic conversions on their operands."
1140 // TODO: Add a test to exercise this part of the code.
1141 rn = convert(NOOP, 0, gettyp(INT), rn);
1142 return build_op(op, sys, ln->tn_type, ln, rn);
1143 }
1144
1145 static bool
1146 is_null_pointer(const tnode_t *tn)
1147 {
1148 tspec_t t = tn->tn_type->t_tspec;
1149
1150 // TODO: Investigate how other pointers are stored, in particular,
1151 // whether a pointer constant can have a non-zero value.
1152 // If not, simplify the code below.
1153 return ((t == PTR && tn->tn_type->t_subt->t_tspec == VOID)
1154 || is_integer(t))
1155 && (tn->tn_op == CON && tn->u.value.u.integer == 0);
1156 }
1157
1158 /* Return a type based on tp1, with added qualifiers from tp2. */
1159 static type_t *
1160 merge_qualifiers(type_t *tp1, const type_t *tp2)
1161 {
1162
1163 lint_assert(tp1->t_tspec == PTR);
1164 lint_assert(tp2->t_tspec == PTR);
1165
1166 bool c1 = tp1->t_subt->t_const;
1167 bool c2 = tp2->t_subt->t_const;
1168 bool v1 = tp1->t_subt->t_volatile;
1169 bool v2 = tp2->t_subt->t_volatile;
1170
1171 if (c1 == (c1 | c2) && v1 == (v1 | v2))
1172 return tp1;
1173
1174 type_t *nstp = expr_dup_type(tp1->t_subt);
1175 nstp->t_const |= c2;
1176 nstp->t_volatile |= v2;
1177
1178 type_t *ntp = expr_dup_type(tp1);
1179 ntp->t_subt = nstp;
1180 return ntp;
1181 }
1182
1183 /* See C99 6.5.15 "Conditional operator". */
1184 static tnode_t *
1185 build_colon(bool sys, tnode_t *ln, tnode_t *rn)
1186 {
1187 tspec_t lt = ln->tn_type->t_tspec;
1188 tspec_t rt = rn->tn_type->t_tspec;
1189
1190 type_t *tp;
1191 if (is_arithmetic(lt) && is_arithmetic(rt))
1192 /* The operands were already balanced in build_binary. */
1193 tp = ln->tn_type;
1194 else if (lt == BOOL && rt == BOOL)
1195 tp = ln->tn_type;
1196 else if (lt == VOID || rt == VOID)
1197 tp = gettyp(VOID);
1198 else if (is_struct_or_union(lt)) {
1199 lint_assert(is_struct_or_union(rt));
1200 lint_assert(ln->tn_type->u.sou == rn->tn_type->u.sou);
1201 if (is_incomplete(ln->tn_type)) {
1202 /* unknown operand size, op '%s' */
1203 error(138, op_name(COLON));
1204 return NULL;
1205 }
1206 tp = ln->tn_type;
1207 } else if (lt == PTR && is_integer(rt)) {
1208 if (rt != PTRDIFF_TSPEC)
1209 rn = convert(NOOP, 0, gettyp(PTRDIFF_TSPEC), rn);
1210 tp = ln->tn_type;
1211 } else if (rt == PTR && is_integer(lt)) {
1212 if (lt != PTRDIFF_TSPEC)
1213 ln = convert(NOOP, 0, gettyp(PTRDIFF_TSPEC), ln);
1214 tp = rn->tn_type;
1215 } else if (lt == PTR && is_null_pointer(rn))
1216 tp = merge_qualifiers(ln->tn_type, rn->tn_type);
1217 else if (rt == PTR && is_null_pointer(ln))
1218 tp = merge_qualifiers(rn->tn_type, ln->tn_type);
1219 else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID)
1220 tp = merge_qualifiers(ln->tn_type, rn->tn_type);
1221 else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID)
1222 tp = merge_qualifiers(rn->tn_type, ln->tn_type);
1223 else {
1224 /*
1225 * XXX For now we simply take the left type. This is probably
1226 * wrong, if one type contains a function prototype and the
1227 * other one, at the same place, only an old-style declaration.
1228 */
1229 tp = merge_qualifiers(ln->tn_type, rn->tn_type);
1230 }
1231
1232 return build_op(COLON, sys, tp, ln, rn);
1233 }
1234
1235 /* TODO: check for varargs */
1236 static bool
1237 is_cast_redundant(const tnode_t *tn)
1238 {
1239 const type_t *ntp = tn->tn_type, *otp = tn->u.ops.left->tn_type;
1240 tspec_t nt = ntp->t_tspec, ot = otp->t_tspec;
1241
1242 if (nt == BOOL || ot == BOOL)
1243 return nt == BOOL && ot == BOOL;
1244
1245 if (is_integer(nt) && is_integer(ot)) {
1246 unsigned int nw = width_in_bits(ntp), ow = width_in_bits(otp);
1247 if (is_uinteger(nt) == is_uinteger(ot))
1248 return nw >= ow;
1249 return is_uinteger(ot) && nw > ow;
1250 }
1251
1252 if (is_complex(nt) || is_complex(ot))
1253 return is_complex(nt) && is_complex(ot) &&
1254 size_in_bits(nt) >= size_in_bits(ot);
1255
1256 if (is_floating(nt) && is_floating(ot))
1257 return size_in_bits(nt) >= size_in_bits(ot);
1258
1259 if (nt == PTR && ot == PTR) {
1260 if (!ntp->t_subt->t_const && otp->t_subt->t_const)
1261 return false;
1262 if (!ntp->t_subt->t_volatile && otp->t_subt->t_volatile)
1263 return false;
1264
1265 if (ntp->t_subt->t_tspec == VOID ||
1266 otp->t_subt->t_tspec == VOID ||
1267 types_compatible(ntp->t_subt, otp->t_subt,
1268 false, false, NULL))
1269 return true;
1270 }
1271
1272 return false;
1273 }
1274
1275 static bool
1276 is_assignment(op_t op)
1277 {
1278
1279 return op == ASSIGN ||
1280 op == MULASS ||
1281 op == DIVASS ||
1282 op == MODASS ||
1283 op == ADDASS ||
1284 op == SUBASS ||
1285 op == SHLASS ||
1286 op == SHRASS ||
1287 op == ANDASS ||
1288 op == XORASS ||
1289 op == ORASS ||
1290 op == RETURN ||
1291 op == INIT;
1292 }
1293
1294 static tnode_t *
1295 build_assignment(op_t op, bool sys, tnode_t *ln, tnode_t *rn)
1296 {
1297
1298 tspec_t lt = ln->tn_type->t_tspec;
1299 tspec_t rt = rn->tn_type->t_tspec;
1300
1301 if (any_query_enabled && is_assignment(rn->tn_op)) {
1302 /* chained assignment with '%s' and '%s' */
1303 query_message(10, op_name(op), op_name(rn->tn_op));
1304 }
1305
1306 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
1307 lint_assert(is_integer(rt));
1308 tnode_t *ctn = subt_size_in_bytes(ln->tn_type);
1309 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
1310 rn = convert(NOOP, 0, ctn->tn_type, rn);
1311 rn = build_op(MULT, sys, rn->tn_type, rn, ctn);
1312 if (rn->u.ops.left->tn_op == CON)
1313 rn = fold_constant_integer(rn);
1314 }
1315
1316 if ((op == ASSIGN || op == RETURN || op == INIT) &&
1317 (lt == STRUCT || rt == STRUCT)) {
1318 lint_assert(lt == rt);
1319 lint_assert(ln->tn_type->u.sou == rn->tn_type->u.sou);
1320 if (is_incomplete(ln->tn_type)) {
1321 if (op == RETURN)
1322 /* cannot return incomplete type */
1323 error(212);
1324 else
1325 /* unknown operand size, op '%s' */
1326 error(138, op_name(op));
1327 return NULL;
1328 }
1329 }
1330
1331 if (op == SHLASS && hflag && allow_trad && allow_c90
1332 && portable_rank_cmp(lt, rt) < 0)
1333 /* semantics of '%s' change in C90; ... */
1334 warning(118, "<<=");
1335
1336 if (op != SHLASS && op != SHRASS
1337 && (op == ASSIGN || lt != PTR)
1338 && (lt != rt || (ln->tn_type->t_bitfield && rn->tn_op == CON))) {
1339 rn = convert(op, 0, ln->tn_type, rn);
1340 rt = lt;
1341 }
1342
1343 if (any_query_enabled && rn->tn_op == CVT && rn->tn_cast &&
1344 types_compatible(ln->tn_type, rn->tn_type, false, false, NULL) &&
1345 is_cast_redundant(rn)) {
1346 /* redundant cast from '%s' to '%s' before assignment */
1347 query_message(7, type_name(rn->u.ops.left->tn_type),
1348 type_name(rn->tn_type));
1349 }
1350
1351 return build_op(op, sys, ln->tn_type, ln, rn);
1352 }
1353
1354 static tnode_t *
1355 build_real_imag(op_t op, bool sys, tnode_t *ln)
1356 {
1357
1358 lint_assert(ln != NULL);
1359 if (ln->tn_op == NAME) {
1360 /*
1361 * This may be too much, but it avoids wrong warnings. See
1362 * d_c99_complex_split.c.
1363 */
1364 mark_as_used(ln->u.sym, false, false);
1365 mark_as_set(ln->u.sym);
1366 }
1367
1368 tspec_t t;
1369 switch (ln->tn_type->t_tspec) {
1370 case LCOMPLEX:
1371 t = LDOUBLE;
1372 break;
1373 case DCOMPLEX:
1374 t = DOUBLE;
1375 break;
1376 case FCOMPLEX:
1377 t = FLOAT;
1378 break;
1379 default:
1380 /* '__%s__' is illegal for type '%s' */
1381 error(276, op == REAL ? "real" : "imag",
1382 type_name(ln->tn_type));
1383 return NULL;
1384 }
1385
1386 tnode_t *ntn = build_op(op, sys, gettyp(t), ln, NULL);
1387 ntn->tn_lvalue = true;
1388 return ntn;
1389 }
1390
1391 static bool
1392 is_confusing_precedence(op_t op, op_t lop, bool lparen, op_t rop, bool rparen)
1393 {
1394
1395 if (op == SHL || op == SHR) {
1396 if (!lparen && (lop == PLUS || lop == MINUS))
1397 return true;
1398 if (!rparen && (rop == PLUS || rop == MINUS))
1399 return true;
1400 return false;
1401 }
1402
1403 if (op == LOGOR) {
1404 if (!lparen && lop == LOGAND)
1405 return true;
1406 if (!rparen && rop == LOGAND)
1407 return true;
1408 return false;
1409 }
1410
1411 lint_assert(op == BITAND || op == BITXOR || op == BITOR);
1412 if (!lparen && lop != op) {
1413 if (lop == PLUS || lop == MINUS)
1414 return true;
1415 if (lop == BITAND || lop == BITXOR)
1416 return true;
1417 }
1418 if (!rparen && rop != op) {
1419 if (rop == PLUS || rop == MINUS)
1420 return true;
1421 if (rop == BITAND || rop == BITXOR)
1422 return true;
1423 }
1424 return false;
1425 }
1426
1427 /*
1428 * Print a warning if the given node has operands which should be
1429 * parenthesized.
1430 *
1431 * XXX Does not work if an operand is a constant expression. Constant
1432 * expressions are already folded.
1433 */
1434 static void
1435 check_precedence_confusion(tnode_t *tn)
1436 {
1437 tnode_t *ln, *rn;
1438
1439 if (!hflag)
1440 return;
1441
1442 debug_node(tn);
1443
1444 lint_assert(is_binary(tn));
1445 for (ln = tn->u.ops.left; ln->tn_op == CVT; ln = ln->u.ops.left)
1446 continue;
1447 for (rn = tn->u.ops.right; rn->tn_op == CVT; rn = rn->u.ops.left)
1448 continue;
1449
1450 if (is_confusing_precedence(tn->tn_op,
1451 ln->tn_op, ln->tn_parenthesized,
1452 rn->tn_op, rn->tn_parenthesized)) {
1453 /* precedence confusion possible: parenthesize! */
1454 warning(169);
1455 }
1456 }
1457
1458 static tnode_t *
1459 fold_constant_compare_zero(tnode_t *tn)
1460 {
1461
1462 val_t *v = xcalloc(1, sizeof(*v));
1463 v->v_tspec = tn->tn_type->t_tspec;
1464 lint_assert(v->v_tspec == INT || (Tflag && v->v_tspec == BOOL));
1465
1466 lint_assert(has_operands(tn));
1467 bool l = constant_is_nonzero(tn->u.ops.left);
1468 bool r = is_binary(tn) && constant_is_nonzero(tn->u.ops.right);
1469
1470 switch (tn->tn_op) {
1471 case NOT:
1472 if (hflag && !suppress_constcond)
1473 /* constant operand to '!' */
1474 warning(239);
1475 v->u.integer = !l ? 1 : 0;
1476 break;
1477 case LOGAND:
1478 v->u.integer = l && r ? 1 : 0;
1479 break;
1480 case LOGOR:
1481 v->u.integer = l || r ? 1 : 0;
1482 break;
1483 default:
1484 lint_assert(/*CONSTCOND*/false);
1485 }
1486
1487 return build_constant(tn->tn_type, v);
1488 }
1489
1490 static long double
1491 floating_error_value(tspec_t t, long double lv)
1492 {
1493 if (t == FLOAT)
1494 return lv < 0 ? -FLT_MAX : FLT_MAX;
1495 if (t == DOUBLE)
1496 return lv < 0 ? -DBL_MAX : DBL_MAX;
1497 /*
1498 * When NetBSD is cross-built in MKLINT=yes mode on x86_64 for sparc64,
1499 * tools/lint checks this code while building usr.bin/xlint. In that
1500 * situation, lint uses the preprocessor for sparc64, in which the type
1501 * 'long double' is IEEE-754-binary128, affecting the macro LDBL_MAX
1502 * below. The type 'long double', as well as the strtold
1503 * implementation, comes from the host platform x86_64 though, where
1504 * 'long double' consumes 128 bits as well but only uses 80 of them.
1505 * The exponent range of the two 'long double' types is the same, but
1506 * the maximum finite value differs due to the extended precision on
1507 * sparc64.
1508 *
1509 * To properly handle the data types of the target platform, lint would
1510 * have to implement the floating-point types in a platform-independent
1511 * way, which is not worth the effort, given how few programs
1512 * practically use 'long double'.
1513 */
1514 /* LINTED 248: floating-point constant out of range */
1515 long double max = LDBL_MAX;
1516 return lv < 0 ? -max : max;
1517 }
1518
1519 static bool
1520 is_floating_overflow(tspec_t t, long double val)
1521 {
1522 if (fpe != 0 || isfinite(val) == 0)
1523 return true;
1524 if (t == FLOAT && (val > FLT_MAX || val < -FLT_MAX))
1525 return true;
1526 if (t == DOUBLE && (val > DBL_MAX || val < -DBL_MAX))
1527 return true;
1528 return false;
1529 }
1530
1531 static tnode_t *
1532 fold_constant_floating(tnode_t *tn)
1533 {
1534
1535 fpe = 0;
1536
1537 tspec_t t = tn->tn_type->t_tspec;
1538
1539 val_t *v = xcalloc(1, sizeof(*v));
1540 v->v_tspec = t;
1541
1542 lint_assert(is_floating(t));
1543 lint_assert(has_operands(tn));
1544 lint_assert(t == tn->u.ops.left->tn_type->t_tspec);
1545 lint_assert(!is_binary(tn) || t == tn->u.ops.right->tn_type->t_tspec);
1546
1547 long double lv = tn->u.ops.left->u.value.u.floating;
1548 long double rv = is_binary(tn) ? tn->u.ops.right->u.value.u.floating
1549 : 0.0;
1550
1551 switch (tn->tn_op) {
1552 case UPLUS:
1553 v->u.floating = lv;
1554 break;
1555 case UMINUS:
1556 v->u.floating = -lv;
1557 break;
1558 case MULT:
1559 v->u.floating = lv * rv;
1560 break;
1561 case DIV:
1562 if (rv == 0.0) {
1563 /* division by 0 */
1564 error(139);
1565 v->u.floating = floating_error_value(t, lv);
1566 } else {
1567 v->u.floating = lv / rv;
1568 }
1569 break;
1570 case PLUS:
1571 v->u.floating = lv + rv;
1572 break;
1573 case MINUS:
1574 v->u.floating = lv - rv;
1575 break;
1576 case LT:
1577 v->u.integer = lv < rv ? 1 : 0;
1578 break;
1579 case LE:
1580 v->u.integer = lv <= rv ? 1 : 0;
1581 break;
1582 case GE:
1583 v->u.integer = lv >= rv ? 1 : 0;
1584 break;
1585 case GT:
1586 v->u.integer = lv > rv ? 1 : 0;
1587 break;
1588 case EQ:
1589 v->u.integer = lv == rv ? 1 : 0;
1590 break;
1591 case NE:
1592 v->u.integer = lv != rv ? 1 : 0;
1593 break;
1594 default:
1595 lint_assert(/*CONSTCOND*/false);
1596 }
1597
1598 // XXX: Must not access u.floating after setting u.integer.
1599 lint_assert(fpe != 0 || isnan(v->u.floating) == 0);
1600 if (is_complex(v->v_tspec)) {
1601 /*
1602 * Don't warn, as lint doesn't model the imaginary part of
1603 * complex numbers.
1604 */
1605 fpe = 0;
1606 } else if (is_floating_overflow(t, v->u.floating)) {
1607 /* operator '%s' produces floating point overflow */
1608 warning(142, op_name(tn->tn_op));
1609 v->u.floating = floating_error_value(t, v->u.floating);
1610 fpe = 0;
1611 }
1612
1613 return build_constant(tn->tn_type, v);
1614 }
1615
1616 static void
1617 use(const tnode_t *tn)
1618 {
1619 if (tn == NULL)
1620 return;
1621 switch (tn->tn_op) {
1622 case NAME:
1623 mark_as_used(tn->u.sym, false /* XXX */, false /* XXX */);
1624 break;
1625 case CON:
1626 case STRING:
1627 break;
1628 case CALL:
1629 case ICALL:;
1630 const function_call *call = tn->u.call;
1631 if (call->args != NULL)
1632 for (size_t i = 0, n = call->args_len; i < n; i++)
1633 use(call->args[i]);
1634 break;
1635 default:
1636 lint_assert(has_operands(tn));
1637 use(tn->u.ops.left);
1638 if (is_binary(tn))
1639 use(tn->u.ops.right);
1640 }
1641 }
1642
1643 /*
1644 * Create a tree node for a binary operator and its two operands. Also called
1645 * for unary operators; in that case rn is NULL.
1646 *
1647 * Function calls, sizeof and casts are handled elsewhere.
1648 */
1649 tnode_t *
1650 build_binary(tnode_t *ln, op_t op, bool sys, tnode_t *rn)
1651 {
1652 const mod_t *mp = &modtab[op];
1653
1654 /* If there was an error in one of the operands, return. */
1655 if (ln == NULL || (mp->m_binary && rn == NULL))
1656 return NULL;
1657
1658 if (mp->m_value_context || mp->m_compares_with_zero)
1659 ln = cconv(ln);
1660 if (mp->m_binary && op != ARROW && op != POINT)
1661 rn = cconv(rn);
1662
1663 if (mp->m_comparison)
1664 check_integer_comparison(op, ln, rn);
1665
1666 if (mp->m_value_context || mp->m_compares_with_zero)
1667 ln = promote(op, false, ln);
1668 if (mp->m_binary && op != ARROW && op != POINT &&
1669 op != ASSIGN && op != RETURN && op != INIT)
1670 rn = promote(op, false, rn);
1671
1672 if (mp->m_warn_if_left_unsigned_in_c90 &&
1673 ln->tn_op == CON && ln->u.value.v_unsigned_since_c90) {
1674 /* C90 treats constant as unsigned, op '%s' */
1675 warning(218, op_name(op));
1676 ln->u.value.v_unsigned_since_c90 = false;
1677 }
1678 if (mp->m_warn_if_right_unsigned_in_c90 &&
1679 rn->tn_op == CON && rn->u.value.v_unsigned_since_c90) {
1680 /* C90 treats constant as unsigned, op '%s' */
1681 warning(218, op_name(op));
1682 rn->u.value.v_unsigned_since_c90 = false;
1683 }
1684
1685 if (mp->m_balance_operands || (!allow_c90 && (op == SHL || op == SHR)))
1686 balance(op, &ln, &rn);
1687
1688 if (!typeok(op, 0, ln, rn))
1689 return NULL;
1690
1691 tnode_t *ntn;
1692 switch (op) {
1693 case POINT:
1694 case ARROW:
1695 ntn = build_struct_access(op, sys, ln, rn);
1696 break;
1697 case INCAFT:
1698 case DECAFT:
1699 case INCBEF:
1700 case DECBEF:
1701 ntn = build_prepost_incdec(op, sys, ln);
1702 break;
1703 case ADDR:
1704 ntn = build_address(sys, ln, false);
1705 break;
1706 case INDIR:
1707 ntn = build_op(INDIR, sys, ln->tn_type->t_subt, ln, NULL);
1708 break;
1709 case PLUS:
1710 case MINUS:
1711 ntn = build_plus_minus(op, sys, ln, rn);
1712 break;
1713 case SHL:
1714 case SHR:
1715 ntn = build_bit_shift(op, sys, ln, rn);
1716 break;
1717 case COLON:
1718 ntn = build_colon(sys, ln, rn);
1719 break;
1720 case ASSIGN:
1721 case MULASS:
1722 case DIVASS:
1723 case MODASS:
1724 case ADDASS:
1725 case SUBASS:
1726 case SHLASS:
1727 case SHRASS:
1728 case ANDASS:
1729 case XORASS:
1730 case ORASS:
1731 case RETURN:
1732 case INIT:
1733 ntn = build_assignment(op, sys, ln, rn);
1734 break;
1735 case COMMA:
1736 if (any_query_enabled) {
1737 /* comma operator with types '%s' and '%s' */
1738 query_message(12,
1739 type_name(ln->tn_type), type_name(rn->tn_type));
1740 }
1741 /* FALLTHROUGH */
1742 case QUEST:
1743 ntn = build_op(op, sys, rn->tn_type, ln, rn);
1744 break;
1745 case REAL:
1746 case IMAG:
1747 ntn = build_real_imag(op, sys, ln);
1748 break;
1749 default:
1750 lint_assert(mp->m_binary == (rn != NULL));
1751 type_t *rettp = mp->m_returns_bool
1752 ? gettyp(Tflag ? BOOL : INT) : ln->tn_type;
1753 ntn = build_op(op, sys, rettp, ln, rn);
1754 break;
1755 }
1756
1757 if (ntn == NULL)
1758 return NULL;
1759
1760 if (mp->m_possible_precedence_confusion)
1761 check_precedence_confusion(ntn);
1762
1763 if (hflag && !suppress_constcond &&
1764 mp->m_compares_with_zero &&
1765 (ln->tn_op == CON ||
1766 (mp->m_binary && op != QUEST && rn->tn_op == CON)) &&
1767 /* XXX: rn->tn_system_dependent should be checked as well */
1768 !ln->tn_system_dependent)
1769 /* constant in conditional context */
1770 warning(161);
1771
1772 if (mp->m_fold_constant_operands && ln->tn_op == CON) {
1773 if (!mp->m_binary || rn->tn_op == CON) {
1774 if (mp->m_compares_with_zero)
1775 ntn = fold_constant_compare_zero(ntn);
1776 else if (is_floating(ntn->tn_type->t_tspec))
1777 ntn = fold_constant_floating(ntn);
1778 else
1779 ntn = fold_constant_integer(ntn);
1780 } else if (op == QUEST) {
1781 lint_assert(has_operands(rn));
1782 use(ln->u.value.u.integer != 0
1783 ? rn->u.ops.right : rn->u.ops.left);
1784 ntn = ln->u.value.u.integer != 0
1785 ? rn->u.ops.left : rn->u.ops.right;
1786 }
1787 }
1788
1789 return ntn;
1790 }
1791
1792 tnode_t *
1793 build_unary(op_t op, bool sys, tnode_t *tn)
1794 {
1795 return build_binary(tn, op, sys, NULL);
1796 }
1797
1798 static bool
1799 are_members_compatible(const sym_t *a, const sym_t *b)
1800 {
1801 if (a->u.s_member.sm_offset_in_bits != b->u.s_member.sm_offset_in_bits)
1802 return false;
1803
1804 const type_t *atp = a->s_type;
1805 const type_t *btp = b->s_type;
1806 bool w = false;
1807 if (!types_compatible(atp, btp, false, false, &w) && !w)
1808 return false;
1809 if (a->s_bitfield != b->s_bitfield)
1810 return false;
1811 if (a->s_bitfield) {
1812 if (atp->t_bit_field_width != btp->t_bit_field_width)
1813 return false;
1814 if (atp->t_bit_field_offset != btp->t_bit_field_offset)
1815 return false;
1816 }
1817 return true;
1818 }
1819
1820 /*
1821 * Return whether all struct/union members with the same name have the same
1822 * type and offset.
1823 */
1824 static bool
1825 all_members_compatible(const sym_t *msym)
1826 {
1827 for (const sym_t *csym = msym;
1828 csym != NULL; csym = csym->s_symtab_next) {
1829 if (!is_member(csym))
1830 continue;
1831 if (strcmp(msym->s_name, csym->s_name) != 0)
1832 continue;
1833
1834 for (const sym_t *sym = csym->s_symtab_next;
1835 sym != NULL; sym = sym->s_symtab_next) {
1836 if (is_member(sym)
1837 && strcmp(csym->s_name, sym->s_name) == 0
1838 && !are_members_compatible(csym, sym))
1839 return false;
1840 }
1841 }
1842 return true;
1843 }
1844
1845 sym_t *
1846 find_member(const struct_or_union *sou, const char *name)
1847 {
1848 for (sym_t *mem = sou->sou_first_member;
1849 mem != NULL; mem = mem->s_next) {
1850 lint_assert(is_member(mem));
1851 lint_assert(mem->u.s_member.sm_containing_type == sou);
1852 if (strcmp(mem->s_name, name) == 0)
1853 return mem;
1854 }
1855
1856 for (sym_t *mem = sou->sou_first_member;
1857 mem != NULL; mem = mem->s_next) {
1858 if (is_struct_or_union(mem->s_type->t_tspec)
1859 && mem->s_name == unnamed) {
1860 sym_t *nested_mem =
1861 find_member(mem->s_type->u.sou, name);
1862 if (nested_mem != NULL)
1863 return nested_mem;
1864 }
1865 }
1866 return NULL;
1867 }
1868
1869 /*
1870 * Remove the member if it was unknown until now, which means
1871 * that no defined struct or union has a member with the same name.
1872 */
1873 static void
1874 remove_unknown_member(tnode_t *tn, sym_t *msym)
1875 {
1876 /* type '%s' does not have member '%s' */
1877 error(101, type_name(tn->tn_type), msym->s_name);
1878 rmsym(msym);
1879 msym->s_kind = SK_MEMBER;
1880 msym->s_scl = STRUCT_MEMBER;
1881
1882 struct_or_union *sou = expr_zero_alloc(sizeof(*sou),
1883 "struct_or_union");
1884 sou->sou_tag = expr_zero_alloc(sizeof(*sou->sou_tag), "sym");
1885 sou->sou_tag->s_name = unnamed;
1886
1887 msym->u.s_member.sm_containing_type = sou;
1888 /*
1889 * The member sm_offset_in_bits is not needed here since this symbol
1890 * can only be used for error reporting.
1891 */
1892 }
1893
1894 /*
1895 * Returns a symbol which has the same name as 'msym' and is a member of the
1896 * struct or union specified by 'tn'.
1897 */
1898 static sym_t *
1899 struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym)
1900 {
1901
1902 /* Determine the tag type of which msym is expected to be a member. */
1903 const type_t *tp = NULL;
1904 if (op == POINT && is_struct_or_union(tn->tn_type->t_tspec))
1905 tp = tn->tn_type;
1906 if (op == ARROW && tn->tn_type->t_tspec == PTR
1907 && is_struct_or_union(tn->tn_type->t_subt->t_tspec))
1908 tp = tn->tn_type->t_subt;
1909 struct_or_union *sou = tp != NULL ? tp->u.sou : NULL;
1910
1911 if (sou != NULL) {
1912 sym_t *nested_mem = find_member(sou, msym->s_name);
1913 if (nested_mem != NULL)
1914 return nested_mem;
1915 }
1916
1917 if (msym->s_scl == NO_SCL) {
1918 remove_unknown_member(tn, msym);
1919 return msym;
1920 }
1921
1922 bool eq = all_members_compatible(msym);
1923
1924 /*
1925 * Now handle the case in which the left operand refers really to a
1926 * struct/union, but the right operand is not member of it.
1927 */
1928 if (sou != NULL) {
1929 if (eq && !allow_c90)
1930 /* illegal use of member '%s' */
1931 warning(102, msym->s_name);
1932 else
1933 /* illegal use of member '%s' */
1934 error(102, msym->s_name);
1935 return msym;
1936 }
1937
1938 if (eq) {
1939 if (op == POINT) {
1940 if (!allow_c90)
1941 /* left operand of '.' must be struct ... */
1942 warning(103, type_name(tn->tn_type));
1943 else
1944 /* left operand of '.' must be struct ... */
1945 error(103, type_name(tn->tn_type));
1946 } else {
1947 if (!allow_c90 && tn->tn_type->t_tspec == PTR)
1948 /* left operand of '->' must be pointer ... */
1949 warning(104, type_name(tn->tn_type));
1950 else
1951 /* left operand of '->' must be pointer ... */
1952 error(104, type_name(tn->tn_type));
1953 }
1954 } else {
1955 if (!allow_c90)
1956 /* non-unique member requires struct/union %s */
1957 error(105, op == POINT ? "object" : "pointer");
1958 else
1959 /* unacceptable operand of '%s' */
1960 error(111, op_name(op));
1961 }
1962
1963 return msym;
1964 }
1965
1966 tnode_t *
1967 build_member_access(tnode_t *ln, op_t op, bool sys, sbuf_t *member)
1968 {
1969 sym_t *msym;
1970
1971 if (ln == NULL)
1972 return NULL;
1973
1974 if (op == ARROW)
1975 /* must do this before struct_or_union_member is called */
1976 ln = cconv(ln);
1977 msym = struct_or_union_member(ln, op, getsym(member));
1978 return build_binary(ln, op, sys, build_name(msym, false));
1979 }
1980
1981 /*
1982 * Perform class conversions.
1983 *
1984 * Arrays of type T are converted into pointers to type T.
1985 * Functions are converted to pointers to functions.
1986 * Lvalues are converted to rvalues.
1987 *
1988 * C99 6.3 "Conversions"
1989 * C99 6.3.2 "Other operands"
1990 * C99 6.3.2.1 "Lvalues, arrays, and function designators"
1991 */
1992 tnode_t *
1993 cconv(tnode_t *tn)
1994 {
1995 if (tn->tn_type->t_tspec == ARRAY) {
1996 if (!tn->tn_lvalue) {
1997 /* XXX print correct operator */
1998 /* %soperand of '%s' must be lvalue */
1999 gnuism(114, "", op_name(ADDR));
2000 }
2001 tn = build_op(ADDR, tn->tn_sys,
2002 expr_derive_type(tn->tn_type->t_subt, PTR), tn, NULL);
2003 }
2004
2005 if (tn->tn_type->t_tspec == FUNC)
2006 tn = build_address(tn->tn_sys, tn, true);
2007
2008 if (tn->tn_lvalue) {
2009 type_t *tp = expr_dup_type(tn->tn_type);
2010 /* C99 6.3.2.1p2 sentence 2 says to remove the qualifiers. */
2011 tp->t_const = tp->t_volatile = false;
2012 tn = build_op(LOAD, tn->tn_sys, tp, tn, NULL);
2013 }
2014
2015 return tn;
2016 }
2017
2018 const tnode_t *
2019 before_conversion(const tnode_t *tn)
2020 {
2021 while (tn->tn_op == CVT && !tn->tn_cast)
2022 tn = tn->u.ops.left;
2023 return tn;
2024 }
2025
2026 /*
2027 * Most errors required by C90 are reported in struct_or_union_member().
2028 * Here we only check for totally wrong things.
2029 */
2030 static bool
2031 typeok_point(const tnode_t *ln, const type_t *ltp, tspec_t lt)
2032 {
2033 if (is_struct_or_union(lt))
2034 return true;
2035
2036 if (lt == FUNC || lt == VOID || ltp->t_bitfield)
2037 goto wrong;
2038
2039 /*
2040 * Some C dialects from before C90 tolerated any lvalue on the
2041 * left-hand side of the '.' operator, allowing things like 'char
2042 * st[100]; st.st_mtime', assuming that the member 'st_mtime' only
2043 * occurred in a single struct; see typeok_arrow.
2044 */
2045 if (ln->tn_lvalue)
2046 return true;
2047
2048 wrong:
2049 /* With allow_c90 we already got an error */
2050 if (!allow_c90)
2051 /* unacceptable operand of '%s' */
2052 error(111, op_name(POINT));
2053
2054 return false;
2055 }
2056
2057 static bool
2058 typeok_arrow(tspec_t lt)
2059 {
2060 /*
2061 * C1978 Appendix A 14.1 says: <quote>In fact, any lvalue is allowed
2062 * before '.', and that lvalue is then assumed to have the form of the
2063 * structure of which the name of the right is a member. [...] Such
2064 * constructions are non-portable.</quote>
2065 */
2066 if (lt == PTR || (!allow_c90 && is_integer(lt)))
2067 return true;
2068
2069 /* With allow_c90 we already got an error */
2070 if (!allow_c90)
2071 /* unacceptable operand of '%s' */
2072 error(111, op_name(ARROW));
2073 return false;
2074 }
2075
2076 static bool
2077 typeok_incdec(op_t op, const tnode_t *tn, const type_t *tp)
2078 {
2079 /* operand has scalar type (checked in typeok) */
2080 if (!tn->tn_lvalue) {
2081 if (tn->tn_op == CVT && tn->tn_cast &&
2082 tn->u.ops.left->tn_op == LOAD)
2083 /* a cast does not yield an lvalue */
2084 error(163);
2085 /* %soperand of '%s' must be lvalue */
2086 error(114, "", op_name(op));
2087 return false;
2088 }
2089 if (tp->t_const && allow_c90)
2090 /* %soperand of '%s' must be modifiable lvalue */
2091 warning(115, "", op_name(op));
2092 return true;
2093 }
2094
2095 static bool
2096 typeok_address(op_t op, const tnode_t *tn, const type_t *tp, tspec_t t)
2097 {
2098 if (t == ARRAY || t == FUNC) {
2099 /* ok, a warning comes later (in build_address()) */
2100 } else if (!tn->tn_lvalue) {
2101 if (tn->tn_op == CVT && tn->tn_cast &&
2102 tn->u.ops.left->tn_op == LOAD)
2103 /* a cast does not yield an lvalue */
2104 error(163);
2105 /* %soperand of '%s' must be lvalue */
2106 error(114, "", op_name(op));
2107 return false;
2108 } else if (is_scalar(t)) {
2109 if (tp->t_bitfield) {
2110 /* cannot take address of bit-field */
2111 error(112);
2112 return false;
2113 }
2114 } else if (t != STRUCT && t != UNION) {
2115 /* unacceptable operand of '%s' */
2116 error(111, op_name(op));
2117 return false;
2118 }
2119 if (tn->tn_op == NAME && tn->u.sym->s_register) {
2120 /* cannot take address of register '%s' */
2121 error(113, tn->u.sym->s_name);
2122 return false;
2123 }
2124 return true;
2125 }
2126
2127 static bool
2128 typeok_indir(const type_t *tp, tspec_t t)
2129 {
2130
2131 if (t != PTR) {
2132 /* cannot dereference non-pointer type '%s' */
2133 error(96, type_name(tp));
2134 return false;
2135 }
2136 return true;
2137 }
2138
2139 static void
2140 warn_incompatible_types(op_t op,
2141 const type_t *ltp, tspec_t lt,
2142 const type_t *rtp, tspec_t rt)
2143 {
2144 bool binary = modtab[op].m_binary;
2145
2146 if (lt == VOID || (binary && rt == VOID)) {
2147 /* void type illegal in expression */
2148 error(109);
2149 } else if (op == ASSIGN)
2150 /* cannot assign to '%s' from '%s' */
2151 error(171, type_name(ltp), type_name(rtp));
2152 else if (binary)
2153 /* operands of '%s' have incompatible types '%s' and '%s' */
2154 error(107, op_name(op), type_name(ltp), type_name(rtp));
2155 else {
2156 lint_assert(rt == NO_TSPEC);
2157 /* operand of '%s' has invalid type '%s' */
2158 error(108, op_name(op), type_name(ltp));
2159 }
2160 }
2161
2162 static bool
2163 typeok_plus(op_t op,
2164 const type_t *ltp, tspec_t lt,
2165 const type_t *rtp, tspec_t rt)
2166 {
2167 /* operands have scalar types (checked in typeok) */
2168 if ((lt == PTR && !is_integer(rt)) || (rt == PTR && !is_integer(lt))) {
2169 warn_incompatible_types(op, ltp, lt, rtp, rt);
2170 return false;
2171 }
2172 return true;
2173 }
2174
2175 static bool
2176 typeok_minus(op_t op,
2177 const type_t *ltp, tspec_t lt,
2178 const type_t *rtp, tspec_t rt)
2179 {
2180 /* operands have scalar types (checked in typeok) */
2181 if ((lt == PTR && rt != PTR && !is_integer(rt)) ||
2182 (lt != PTR && rt == PTR)) {
2183 warn_incompatible_types(op, ltp, lt, rtp, rt);
2184 return false;
2185 }
2186 if (lt == PTR && rt == PTR &&
2187 !types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
2188 /* illegal pointer subtraction */
2189 error(116);
2190 }
2191 return true;
2192 }
2193
2194 static void
2195 typeok_shr(op_t op,
2196 const tnode_t *ln, tspec_t lt,
2197 const tnode_t *rn, tspec_t rt)
2198 {
2199 tspec_t olt = before_conversion(ln)->tn_type->t_tspec;
2200 tspec_t ort = before_conversion(rn)->tn_type->t_tspec;
2201
2202 /* operands have integer types (checked in typeok) */
2203 if (pflag && !is_uinteger(olt)) {
2204 integer_constraints lc = ic_expr(ln);
2205 if (!ic_maybe_signed(ln->tn_type, &lc))
2206 return;
2207
2208 if (ln->tn_op != CON)
2209 /* bitwise '%s' on signed value possibly nonportable */
2210 warning(117, op_name(op));
2211 else if (ln->u.value.u.integer < 0)
2212 /* bitwise '%s' on signed value nonportable */
2213 warning(120, op_name(op));
2214 } else if (allow_trad && allow_c90 &&
2215 !is_uinteger(olt) && is_uinteger(ort)) {
2216 /* The left operand would become unsigned in traditional C. */
2217 if (hflag && (ln->tn_op != CON || ln->u.value.u.integer < 0)) {
2218 /* semantics of '%s' change in C90; use ... */
2219 warning(118, op_name(op));
2220 }
2221 } else if (allow_trad && allow_c90 &&
2222 !is_uinteger(olt) && !is_uinteger(ort) &&
2223 portable_rank_cmp(lt, rt) < 0) {
2224 /*
2225 * In traditional C, the left operand would be extended
2226 * (possibly sign-extended) and then shifted.
2227 */
2228 if (hflag && (ln->tn_op != CON || ln->u.value.u.integer < 0)) {
2229 /* semantics of '%s' change in C90; use ... */
2230 warning(118, op_name(op));
2231 }
2232 }
2233 }
2234
2235 static void
2236 typeok_shl(op_t op, tspec_t lt, tspec_t rt)
2237 {
2238 /*
2239 * C90 does not perform balancing for shift operations, but traditional
2240 * C does. If the width of the right operand is greater than the width
2241 * of the left operand, then in traditional C the left operand would be
2242 * extended to the width of the right operand. For SHL this may result
2243 * in different results.
2244 */
2245 if (portable_rank_cmp(lt, rt) < 0) {
2246 /*
2247 * XXX If both operands are constant, make sure that there is
2248 * really a difference between C90 and traditional C.
2249 */
2250 if (hflag && allow_trad && allow_c90)
2251 /* semantics of '%s' change in C90; use ... */
2252 warning(118, op_name(op));
2253 }
2254 }
2255
2256 static void
2257 typeok_shift(const type_t *ltp, tspec_t lt, const tnode_t *rn, tspec_t rt)
2258 {
2259 if (rn->tn_op != CON)
2260 return;
2261
2262 if (!is_uinteger(rt) && rn->u.value.u.integer < 0)
2263 /* negative shift */
2264 warning(121);
2265 else if ((uint64_t)rn->u.value.u.integer == size_in_bits(lt))
2266 /* shift amount %u equals bit-size of '%s' */
2267 warning(267, (unsigned)rn->u.value.u.integer, type_name(ltp));
2268 else if ((uint64_t)rn->u.value.u.integer > size_in_bits(lt)) {
2269 /* shift amount %llu is greater than bit-size %llu of '%s' */
2270 warning(122, (unsigned long long)rn->u.value.u.integer,
2271 (unsigned long long)size_in_bits(lt),
2272 tspec_name(lt));
2273 }
2274 }
2275
2276 static bool
2277 is_typeok_eq(const tnode_t *ln, tspec_t lt, const tnode_t *rn, tspec_t rt)
2278 {
2279 if (lt == PTR && is_null_pointer(rn))
2280 return true;
2281 if (rt == PTR && is_null_pointer(ln))
2282 return true;
2283 return false;
2284 }
2285
2286 static void
2287 warn_incompatible_pointers(op_t op, const type_t *ltp, const type_t *rtp)
2288 {
2289 lint_assert(ltp->t_tspec == PTR);
2290 lint_assert(rtp->t_tspec == PTR);
2291
2292 tspec_t lt = ltp->t_subt->t_tspec;
2293 tspec_t rt = rtp->t_subt->t_tspec;
2294
2295 if (is_struct_or_union(lt) && is_struct_or_union(rt)) {
2296 if (op == RETURN)
2297 /* illegal structure pointer combination */
2298 warning(244);
2299 else {
2300 /* incompatible structure pointers: '%s' '%s' '%s' */
2301 warning(245, type_name(ltp),
2302 op_name(op), type_name(rtp));
2303 }
2304 } else {
2305 if (op == RETURN)
2306 /* illegal combination of '%s' and '%s' */
2307 warning(184, type_name(ltp), type_name(rtp));
2308 else {
2309 /* illegal combination of '%s' and '%s', op '%s' */
2310 warning(124,
2311 type_name(ltp), type_name(rtp), op_name(op));
2312 }
2313 }
2314 }
2315
2316 static void
2317 check_pointer_comparison(op_t op, const tnode_t *ln, const tnode_t *rn)
2318 {
2319 type_t *ltp = ln->tn_type, *rtp = rn->tn_type;
2320 tspec_t lst = ltp->t_subt->t_tspec, rst = rtp->t_subt->t_tspec;
2321
2322 if (lst == VOID || rst == VOID) {
2323 /* TODO: C99 behaves like C90 here. */
2324 if (!allow_trad && !allow_c99 &&
2325 (lst == FUNC || rst == FUNC)) {
2326 /* (void *)0 is already handled in typeok() */
2327 const char *lsts, *rsts;
2328 *(lst == FUNC ? &lsts : &rsts) = "function pointer";
2329 *(lst == VOID ? &lsts : &rsts) = "'void *'";
2330 /* C90 or later forbid comparison of %s with %s */
2331 warning(274, lsts, rsts);
2332 }
2333 return;
2334 }
2335
2336 if (!types_compatible(ltp->t_subt, rtp->t_subt, true, false, NULL)) {
2337 warn_incompatible_pointers(op, ltp, rtp);
2338 return;
2339 }
2340
2341 if (lst == FUNC && rst == FUNC) {
2342 /* TODO: C99 behaves like C90 here, see C99 6.5.8p2. */
2343 if (!allow_trad && !allow_c99 && op != EQ && op != NE)
2344 /* pointers to functions can only be compared ... */
2345 warning(125);
2346 }
2347 }
2348
2349 static bool
2350 typeok_compare(op_t op,
2351 const tnode_t *ln, const type_t *ltp, tspec_t lt,
2352 const tnode_t *rn, const type_t *rtp, tspec_t rt)
2353 {
2354 if (lt == PTR && rt == PTR) {
2355 check_pointer_comparison(op, ln, rn);
2356 return true;
2357 }
2358
2359 if (lt != PTR && rt != PTR)
2360 return true;
2361
2362 if (!is_integer(lt) && !is_integer(rt)) {
2363 warn_incompatible_types(op, ltp, lt, rtp, rt);
2364 return false;
2365 }
2366
2367 const char *lx = lt == PTR ? "pointer" : "integer";
2368 const char *rx = rt == PTR ? "pointer" : "integer";
2369 /* illegal combination of %s '%s' and %s '%s', op '%s' */
2370 warning(123, lx, type_name(ltp), rx, type_name(rtp), op_name(op));
2371 return true;
2372 }
2373
2374 static bool
2375 typeok_quest(tspec_t lt, const tnode_t *rn)
2376 {
2377 if (!is_scalar(lt)) {
2378 /* first operand of '?' must have scalar type */
2379 error(170);
2380 return false;
2381 }
2382 lint_assert(before_conversion(rn)->tn_op == COLON);
2383 return true;
2384 }
2385
2386 static void
2387 typeok_colon_pointer(const type_t *ltp, const type_t *rtp)
2388 {
2389 type_t *lstp = ltp->t_subt;
2390 type_t *rstp = rtp->t_subt;
2391 tspec_t lst = lstp->t_tspec;
2392 tspec_t rst = rstp->t_tspec;
2393
2394 if ((lst == VOID && rst == FUNC) || (lst == FUNC && rst == VOID)) {
2395 /* (void *)0 is handled in typeok_colon */
2396 /* TODO: C99 behaves like C90 here. */
2397 if (!allow_trad && !allow_c99)
2398 /* conversion of %s to %s requires a cast, op %s */
2399 warning(305, "function pointer", "'void *'",
2400 op_name(COLON));
2401 return;
2402 }
2403
2404 if (pointer_types_are_compatible(lstp, rstp, true))
2405 return;
2406 if (!types_compatible(lstp, rstp, true, false, NULL))
2407 warn_incompatible_pointers(COLON, ltp, rtp);
2408 }
2409
2410 static bool
2411 typeok_colon(const tnode_t *ln, const type_t *ltp, tspec_t lt,
2412 const tnode_t *rn, const type_t *rtp, tspec_t rt)
2413 {
2414
2415 if (is_arithmetic(lt) && is_arithmetic(rt))
2416 return true;
2417 if (lt == BOOL && rt == BOOL)
2418 return true;
2419
2420 if (lt == STRUCT && rt == STRUCT && ltp->u.sou == rtp->u.sou)
2421 return true;
2422 if (lt == UNION && rt == UNION && ltp->u.sou == rtp->u.sou)
2423 return true;
2424
2425 if (lt == PTR && is_null_pointer(rn))
2426 return true;
2427 if (rt == PTR && is_null_pointer(ln))
2428 return true;
2429
2430 if ((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)) {
2431 const char *lx = lt == PTR ? "pointer" : "integer";
2432 const char *rx = rt == PTR ? "pointer" : "integer";
2433 /* illegal combination of %s '%s' and %s '%s', op '%s' */
2434 warning(123, lx, type_name(ltp),
2435 rx, type_name(rtp), op_name(COLON));
2436 return true;
2437 }
2438
2439 if (lt == VOID || rt == VOID) {
2440 if (lt != VOID || rt != VOID)
2441 /* incompatible types '%s' and '%s' in conditional */
2442 warning(126, type_name(ltp), type_name(rtp));
2443 return true;
2444 }
2445
2446 if (lt == PTR && rt == PTR) {
2447 typeok_colon_pointer(ltp, rtp);
2448 return true;
2449 }
2450
2451 /* incompatible types '%s' and '%s' in conditional */
2452 error(126, type_name(ltp), type_name(rtp));
2453 return false;
2454 }
2455
2456 static bool
2457 has_constant_member(const type_t *tp)
2458 {
2459 lint_assert(is_struct_or_union(tp->t_tspec));
2460
2461 for (sym_t *m = tp->u.sou->sou_first_member;
2462 m != NULL; m = m->s_next) {
2463 const type_t *mtp = m->s_type;
2464 if (mtp->t_const)
2465 return true;
2466 if (is_struct_or_union(mtp->t_tspec) &&
2467 has_constant_member(mtp))
2468 return true;
2469 }
2470 return false;
2471 }
2472
2473 static bool
2474 typeok_assign(op_t op, const tnode_t *ln, const type_t *ltp, tspec_t lt)
2475 {
2476 if (op == RETURN || op == INIT || op == FARG)
2477 return true;
2478
2479 if (!ln->tn_lvalue) {
2480 if (ln->tn_op == CVT && ln->tn_cast &&
2481 ln->u.ops.left->tn_op == LOAD)
2482 /* a cast does not yield an lvalue */
2483 error(163);
2484 /* %soperand of '%s' must be lvalue */
2485 error(114, "left ", op_name(op));
2486 return false;
2487 } else if (ltp->t_const
2488 || (is_struct_or_union(lt) && has_constant_member(ltp))) {
2489 if (allow_c90)
2490 /* %soperand of '%s' must be modifiable lvalue */
2491 warning(115, "left ", op_name(op));
2492 }
2493 return true;
2494 }
2495
2496 static bool
2497 typeok_scalar(op_t op, const mod_t *mp,
2498 const type_t *ltp, tspec_t lt,
2499 const type_t *rtp, tspec_t rt)
2500 {
2501 if (mp->m_takes_bool && lt == BOOL && rt == BOOL)
2502 return true;
2503 if (mp->m_requires_integer) {
2504 if (!is_integer(lt) || (mp->m_binary && !is_integer(rt))) {
2505 warn_incompatible_types(op, ltp, lt, rtp, rt);
2506 return false;
2507 }
2508 } else if (mp->m_requires_integer_or_complex) {
2509 if ((!is_integer(lt) && !is_complex(lt)) ||
2510 (mp->m_binary && (!is_integer(rt) && !is_complex(rt)))) {
2511 warn_incompatible_types(op, ltp, lt, rtp, rt);
2512 return false;
2513 }
2514 } else if (mp->m_requires_scalar) {
2515 if (!is_scalar(lt) || (mp->m_binary && !is_scalar(rt))) {
2516 warn_incompatible_types(op, ltp, lt, rtp, rt);
2517 return false;
2518 }
2519 } else if (mp->m_requires_arith) {
2520 if (!is_arithmetic(lt) ||
2521 (mp->m_binary && !is_arithmetic(rt))) {
2522 warn_incompatible_types(op, ltp, lt, rtp, rt);
2523 return false;
2524 }
2525 }
2526 return true;
2527 }
2528
2529 static void
2530 check_assign_void_pointer(op_t op, int arg,
2531 tspec_t lt, tspec_t lst,
2532 tspec_t rt, tspec_t rst)
2533 {
2534
2535 if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID)))
2536 return;
2537 /* two pointers, at least one pointer to void */
2538
2539 /* TODO: C99 behaves like C90 here. */
2540 if (!(!allow_trad && !allow_c99 && (lst == FUNC || rst == FUNC)))
2541 return;
2542 /* comb. of ptr to func and ptr to void */
2543
2544 const char *lts, *rts;
2545 *(lst == FUNC ? <s : &rts) = "function pointer";
2546 *(lst == VOID ? <s : &rts) = "'void *'";
2547
2548 switch (op) {
2549 case INIT:
2550 case RETURN:
2551 /* conversion of %s to %s requires a cast */
2552 warning(303, rts, lts);
2553 break;
2554 case FARG:
2555 /* conversion of %s to %s requires a cast, arg #%d */
2556 warning(304, rts, lts, arg);
2557 break;
2558 default:
2559 /* conversion of %s to %s requires a cast, op %s */
2560 warning(305, rts, lts, op_name(op));
2561 break;
2562 }
2563 }
2564
2565 static bool
2566 is_direct_function_call(const tnode_t *tn, const char **out_name)
2567 {
2568
2569 if (tn->tn_op == CALL
2570 && tn->u.call->func->tn_op == ADDR
2571 && tn->u.call->func->u.ops.left->tn_op == NAME) {
2572 *out_name = tn->u.call->func->u.ops.left->u.sym->s_name;
2573 return true;
2574 }
2575 return false;
2576 }
2577
2578 static bool
2579 is_unconst_function(const char *name)
2580 {
2581
2582 return strcmp(name, "memchr") == 0 ||
2583 strcmp(name, "strchr") == 0 ||
2584 strcmp(name, "strpbrk") == 0 ||
2585 strcmp(name, "strrchr") == 0 ||
2586 strcmp(name, "strstr") == 0;
2587 }
2588
2589 static bool
2590 is_const_char_pointer(const tnode_t *tn)
2591 {
2592 /*
2593 * For traditional reasons, C99 6.4.5p5 defines that string literals
2594 * have type 'char[]'. They are often implicitly converted to 'char
2595 * *', for example when they are passed as function arguments.
2596 *
2597 * C99 6.4.5p6 further defines that modifying a string that is
2598 * constructed from a string literal invokes undefined behavior.
2599 *
2600 * Out of these reasons, string literals are treated as 'effectively
2601 * const' here.
2602 */
2603 if (tn->tn_op == CVT &&
2604 tn->u.ops.left->tn_op == ADDR &&
2605 tn->u.ops.left->u.ops.left->tn_op == STRING)
2606 return true;
2607
2608 const type_t *tp = before_conversion(tn)->tn_type;
2609 return tp->t_tspec == PTR &&
2610 tp->t_subt->t_tspec == CHAR &&
2611 tp->t_subt->t_const;
2612 }
2613
2614 static bool
2615 is_first_arg_const_char_pointer(const tnode_t *tn)
2616 {
2617 return tn->u.call->args != NULL
2618 && tn->u.call->args_len >= 1
2619 && is_const_char_pointer(tn->u.call->args[0]);
2620 }
2621
2622 static bool
2623 is_const_pointer(const tnode_t *tn)
2624 {
2625 const type_t *tp = before_conversion(tn)->tn_type;
2626 return tp->t_tspec == PTR && tp->t_subt->t_const;
2627 }
2628
2629 static bool
2630 is_second_arg_const_pointer(const tnode_t *tn)
2631 {
2632 return tn->u.call->args_len >= 2
2633 && tn->u.call->args != NULL
2634 && is_const_pointer(tn->u.call->args[1]);
2635 }
2636
2637 static void
2638 check_unconst_function(const type_t *lstp, const tnode_t *rn)
2639 {
2640 const char *function_name;
2641
2642 if (lstp->t_tspec == CHAR && !lstp->t_const &&
2643 is_direct_function_call(rn, &function_name) &&
2644 is_unconst_function(function_name) &&
2645 is_first_arg_const_char_pointer(rn)) {
2646 /* call to '%s' effectively discards 'const' from argument */
2647 warning(346, function_name);
2648 }
2649
2650 if (!lstp->t_const &&
2651 is_direct_function_call(rn, &function_name) &&
2652 strcmp(function_name, "bsearch") == 0 &&
2653 is_second_arg_const_pointer(rn)) {
2654 /* call to '%s' effectively discards 'const' from argument */
2655 warning(346, function_name);
2656 }
2657 }
2658
2659 static bool
2660 check_assign_void_pointer_compat(op_t op, int arg,
2661 const type_t *ltp, tspec_t lt,
2662 const type_t *lstp, tspec_t lst,
2663 const tnode_t *rn,
2664 const type_t *rtp, tspec_t rt,
2665 const type_t *rstp, tspec_t rst)
2666 {
2667 if (!(lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
2668 types_compatible(lstp, rstp,
2669 true, false, NULL))))
2670 return false;
2671
2672 /* compatible pointer types (qualifiers ignored) */
2673 if (allow_c90 &&
2674 ((!lstp->t_const && rstp->t_const) ||
2675 (!lstp->t_volatile && rstp->t_volatile))) {
2676 /* left side has not all qualifiers of right */
2677 switch (op) {
2678 case INIT:
2679 case RETURN:
2680 /* incompatible pointer types to '%s' and '%s' */
2681 warning(182, type_name(lstp), type_name(rstp));
2682 break;
2683 case FARG:
2684 /* converting '%s' to incompatible '%s' ... */
2685 warning(153,
2686 type_name(rtp), type_name(ltp), arg);
2687 break;
2688 default:
2689 /* operands of '%s' have incompatible pointer ... */
2690 warning(128, op_name(op),
2691 type_name(lstp), type_name(rstp));
2692 break;
2693 }
2694 }
2695
2696 if (allow_c90)
2697 check_unconst_function(lstp, rn);
2698
2699 return true;
2700 }
2701
2702 static bool
2703 check_assign_pointer_integer(op_t op, int arg,
2704 const type_t *ltp, tspec_t lt,
2705 const type_t *rtp, tspec_t rt)
2706 {
2707
2708 if (!((lt == PTR && is_integer(rt)) || (is_integer(lt) && rt == PTR)))
2709 return false;
2710
2711 const char *lx = lt == PTR ? "pointer" : "integer";
2712 const char *rx = rt == PTR ? "pointer" : "integer";
2713
2714 switch (op) {
2715 case INIT:
2716 case RETURN:
2717 /* illegal combination of %s '%s' and %s '%s' */
2718 warning(183, lx, type_name(ltp), rx, type_name(rtp));
2719 break;
2720 case FARG:
2721 /* illegal combination of %s '%s' and %s '%s', arg #%d */
2722 warning(154,
2723 lx, type_name(ltp), rx, type_name(rtp), arg);
2724 break;
2725 default:
2726 /* illegal combination of %s '%s' and %s '%s', op '%s' */
2727 warning(123,
2728 lx, type_name(ltp), rx, type_name(rtp), op_name(op));
2729 break;
2730 }
2731 return true;
2732 }
2733
2734 static bool
2735 check_assign_pointer(op_t op, int arg,
2736 const type_t *ltp, tspec_t lt,
2737 const type_t *rtp, tspec_t rt)
2738 {
2739 if (!(lt == PTR && rt == PTR))
2740 return false;
2741
2742 if (op == FARG)
2743 /* converting '%s' to incompatible '%s' for ... */
2744 warning(153, type_name(rtp), type_name(ltp), arg);
2745 else
2746 warn_incompatible_pointers(op, ltp, rtp);
2747 return true;
2748 }
2749
2750 static void
2751 warn_assign(op_t op, int arg,
2752 const type_t *ltp, tspec_t lt,
2753 const type_t *rtp, tspec_t rt)
2754 {
2755 switch (op) {
2756 case INIT:
2757 /* cannot initialize '%s' from '%s' */
2758 error(185, type_name(ltp), type_name(rtp));
2759 break;
2760 case RETURN:
2761 /* function has return type '%s' but returns '%s' */
2762 error(211, type_name(ltp), type_name(rtp));
2763 break;
2764 case FARG:
2765 /* passing '%s' to incompatible '%s', arg #%d */
2766 warning(155, type_name(rtp), type_name(ltp), arg);
2767 break;
2768 default:
2769 warn_incompatible_types(op, ltp, lt, rtp, rt);
2770 break;
2771 }
2772 }
2773
2774 /*
2775 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
2776 * and prints warnings/errors if necessary.
2777 * Returns whether the types are (almost) compatible.
2778 */
2779 static bool
2780 check_assign_types_compatible(op_t op, int arg,
2781 const tnode_t *ln, const tnode_t *rn)
2782 {
2783 tspec_t lt, rt, lst = NO_TSPEC, rst = NO_TSPEC;
2784 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
2785
2786 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
2787 lst = (lstp = ltp->t_subt)->t_tspec;
2788 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
2789 rst = (rstp = rtp->t_subt)->t_tspec;
2790
2791 if (lt == BOOL && is_scalar(rt)) /* C99 6.3.1.2 */
2792 return true;
2793
2794 if (is_arithmetic(lt) && (is_arithmetic(rt) || rt == BOOL))
2795 return true;
2796
2797 if (is_struct_or_union(lt) && is_struct_or_union(rt))
2798 return ltp->u.sou == rtp->u.sou;
2799
2800 if (lt == PTR && is_null_pointer(rn)) {
2801 if (is_integer(rn->tn_type->t_tspec))
2802 /* implicit conversion from integer 0 to pointer ... */
2803 query_message(15, type_name(ltp));
2804 return true;
2805 }
2806
2807 check_assign_void_pointer(op, arg, lt, lst, rt, rst);
2808
2809 if (check_assign_void_pointer_compat(op, arg,
2810 ltp, lt, lstp, lst, rn, rtp, rt, rstp, rst))
2811 return true;
2812
2813 if (check_assign_pointer_integer(op, arg, ltp, lt, rtp, rt))
2814 return true;
2815
2816 if (check_assign_pointer(op, arg, ltp, lt, rtp, rt))
2817 return true;
2818
2819 warn_assign(op, arg, ltp, lt, rtp, rt);
2820 return false;
2821 }
2822
2823 static bool
2824 has_side_effect(const tnode_t *tn) /* NOLINT(misc-no-recursion) */
2825 {
2826 op_t op = tn->tn_op;
2827
2828 if (modtab[op].m_has_side_effect)
2829 return true;
2830
2831 if (op == CVT && tn->tn_type->t_tspec == VOID)
2832 return has_side_effect(tn->u.ops.left);
2833
2834 /* XXX: Why not has_side_effect(tn->u.ops.left) as well? */
2835 if (op == LOGAND || op == LOGOR)
2836 return has_side_effect(tn->u.ops.right);
2837
2838 /* XXX: Why not has_side_effect(tn->u.ops.left) as well? */
2839 if (op == QUEST)
2840 return has_side_effect(tn->u.ops.right);
2841
2842 if (op == COLON || op == COMMA) {
2843 return has_side_effect(tn->u.ops.left) ||
2844 has_side_effect(tn->u.ops.right);
2845 }
2846
2847 return false;
2848 }
2849
2850 static bool
2851 is_void_cast(const tnode_t *tn)
2852 {
2853
2854 return tn->tn_op == CVT && tn->tn_cast &&
2855 tn->tn_type->t_tspec == VOID;
2856 }
2857
2858 static bool
2859 is_local_symbol(const tnode_t *tn)
2860 {
2861
2862 return tn->tn_op == LOAD &&
2863 tn->u.ops.left->tn_op == NAME &&
2864 tn->u.ops.left->u.sym->s_scl == AUTO;
2865 }
2866
2867 static bool
2868 is_int_constant_zero(const tnode_t *tn)
2869 {
2870
2871 return tn->tn_op == CON &&
2872 tn->tn_type->t_tspec == INT &&
2873 tn->u.value.u.integer == 0;
2874 }
2875
2876 static void
2877 check_null_effect(const tnode_t *tn)
2878 {
2879
2880 if (hflag &&
2881 !has_side_effect(tn) &&
2882 !(is_void_cast(tn) && is_local_symbol(tn->u.ops.left)) &&
2883 !(is_void_cast(tn) && is_int_constant_zero(tn->u.ops.left))) {
2884 /* expression has null effect */
2885 warning(129);
2886 }
2887 }
2888
2889 /*
2890 * Check the types for specific operators and type combinations.
2891 *
2892 * At this point, the operands already conform to the type requirements of
2893 * the operator, such as being integer, floating or scalar.
2894 */
2895 static bool
2896 typeok_op(op_t op, int arg,
2897 const tnode_t *ln, const type_t *ltp, tspec_t lt,
2898 const tnode_t *rn, const type_t *rtp, tspec_t rt)
2899 {
2900 switch (op) {
2901 case ARROW:
2902 return typeok_arrow(lt);
2903 case POINT:
2904 return typeok_point(ln, ltp, lt);
2905 case INCBEF:
2906 case DECBEF:
2907 case INCAFT:
2908 case DECAFT:
2909 return typeok_incdec(op, ln, ltp);
2910 case INDIR:
2911 return typeok_indir(ltp, lt);
2912 case ADDR:
2913 return typeok_address(op, ln, ltp, lt);
2914 case PLUS:
2915 return typeok_plus(op, ltp, lt, rtp, rt);
2916 case MINUS:
2917 return typeok_minus(op, ltp, lt, rtp, rt);
2918 case SHL:
2919 typeok_shl(op, lt, rt);
2920 goto shift;
2921 case SHR:
2922 typeok_shr(op, ln, lt, rn, rt);
2923 shift:
2924 typeok_shift(ltp, lt, rn, rt);
2925 break;
2926 case LT:
2927 case LE:
2928 case GT:
2929 case GE:
2930 compare:
2931 return typeok_compare(op, ln, ltp, lt, rn, rtp, rt);
2932 case EQ:
2933 case NE:
2934 if (is_typeok_eq(ln, lt, rn, rt))
2935 break;
2936 goto compare;
2937 case QUEST:
2938 return typeok_quest(lt, rn);
2939 case COLON:
2940 return typeok_colon(ln, ltp, lt, rn, rtp, rt);
2941 case ASSIGN:
2942 case INIT:
2943 case FARG:
2944 case RETURN:
2945 if (!check_assign_types_compatible(op, arg, ln, rn))
2946 return false;
2947 goto assign;
2948 case MULASS:
2949 case DIVASS:
2950 case MODASS:
2951 goto assign;
2952 case ADDASS:
2953 case SUBASS:
2954 if ((lt == PTR && !is_integer(rt)) || rt == PTR) {
2955 warn_incompatible_types(op, ltp, lt, rtp, rt);
2956 return false;
2957 }
2958 goto assign;
2959 case SHLASS:
2960 goto assign;
2961 case SHRASS:
2962 if (pflag && !is_uinteger(lt) &&
2963 !(!allow_c90 && is_uinteger(rt))) {
2964 /* bitwise '%s' on signed value possibly nonportable */
2965 warning(117, op_name(op));
2966 }
2967 goto assign;
2968 case ANDASS:
2969 case XORASS:
2970 case ORASS:
2971 assign:
2972 return typeok_assign(op, ln, ltp, lt);
2973 case COMMA:
2974 if (!modtab[ln->tn_op].m_has_side_effect)
2975 check_null_effect(ln);
2976 break;
2977 default:
2978 break;
2979 }
2980 return true;
2981 }
2982
2983 static void
2984 check_bad_enum_operation(op_t op, const tnode_t *ln, const tnode_t *rn)
2985 {
2986
2987 if (!eflag)
2988 return;
2989
2990 /* Allow enum in array indices. */
2991 if (op == PLUS &&
2992 ((ln->tn_type->t_is_enum && rn->tn_type->t_tspec == PTR) ||
2993 (rn->tn_type->t_is_enum && ln->tn_type->t_tspec == PTR))) {
2994 return;
2995 }
2996
2997 /* dubious operation '%s' on enum */
2998 warning(241, op_name(op));
2999 }
3000
3001 static void
3002 check_enum_type_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
3003 {
3004 const mod_t *mp = &modtab[op];
3005
3006 if (ln->tn_type->u.enumer != rn->tn_type->u.enumer) {
3007 switch (op) {
3008 case INIT:
3009 /* enum type mismatch between '%s' and '%s' in ... */
3010 warning(210,
3011 type_name(ln->tn_type), type_name(rn->tn_type));
3012 break;
3013 case FARG:
3014 /* function expects '%s', passing '%s' for arg #%d */
3015 warning(156,
3016 type_name(ln->tn_type), type_name(rn->tn_type),
3017 arg);
3018 break;
3019 case RETURN:
3020 /* function has return type '%s' but returns '%s' */
3021 warning(211,
3022 type_name(ln->tn_type), type_name(rn->tn_type));
3023 break;
3024 default:
3025 /* enum type mismatch: '%s' '%s' '%s' */
3026 warning(130, type_name(ln->tn_type), op_name(op),
3027 type_name(rn->tn_type));
3028 break;
3029 }
3030 } else if (Pflag && eflag && mp->m_comparison && op != EQ && op != NE)
3031 /* operator '%s' assumes that '%s' is ordered */
3032 warning(243, op_name(op), type_name(ln->tn_type));
3033 }
3034
3035 static void
3036 check_enum_int_mismatch(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
3037 {
3038
3039 if (!eflag)
3040 return;
3041
3042 switch (op) {
3043 case INIT:
3044 /*
3045 * Initialization with 0 is allowed. Otherwise, all implicit
3046 * initializations would need to be warned upon as well.
3047 */
3048 if (!rn->tn_type->t_is_enum && rn->tn_op == CON &&
3049 is_integer(rn->tn_type->t_tspec) &&
3050 rn->u.value.u.integer == 0) {
3051 return;
3052 }
3053 /* initialization of '%s' with '%s' */
3054 warning(277, type_name(ln->tn_type), type_name(rn->tn_type));
3055 break;
3056 case FARG:
3057 /* combination of '%s' and '%s', arg #%d */
3058 warning(278,
3059 type_name(ln->tn_type), type_name(rn->tn_type), arg);
3060 break;
3061 case RETURN:
3062 /* combination of '%s' and '%s' in return */
3063 warning(279, type_name(ln->tn_type), type_name(rn->tn_type));
3064 break;
3065 default:
3066 /* combination of '%s' and '%s', op '%s' */
3067 warning(242, type_name(ln->tn_type), type_name(rn->tn_type),
3068 op_name(op));
3069 break;
3070 }
3071 }
3072
3073 static void
3074 typeok_enum(op_t op, const mod_t *mp, int arg,
3075 const tnode_t *ln, const type_t *ltp,
3076 const tnode_t *rn, const type_t *rtp)
3077 {
3078 if (mp->m_bad_on_enum &&
3079 (ltp->t_is_enum || (mp->m_binary && rtp->t_is_enum))) {
3080 check_bad_enum_operation(op, ln, rn);
3081 } else if (mp->m_valid_on_enum &&
3082 (ltp->t_is_enum && rtp != NULL && rtp->t_is_enum)) {
3083 check_enum_type_mismatch(op, arg, ln, rn);
3084 } else if (mp->m_valid_on_enum &&
3085 (ltp->t_is_enum || (rtp != NULL && rtp->t_is_enum))) {
3086 check_enum_int_mismatch(op, arg, ln, rn);
3087 }
3088 }
3089
3090 /* Perform most type checks. Return whether the types are ok. */
3091 bool
3092 typeok(op_t op, int arg, const tnode_t *ln, const tnode_t *rn)
3093 {
3094
3095 const mod_t *mp = &modtab[op];
3096
3097 type_t *ltp = ln->tn_type;
3098 tspec_t lt = ltp->t_tspec;
3099
3100 type_t *rtp = mp->m_binary ? rn->tn_type : NULL;
3101 tspec_t rt = mp->m_binary ? rtp->t_tspec : NO_TSPEC;
3102
3103 if (Tflag && !typeok_scalar_strict_bool(op, mp, arg, ln, rn))
3104 return false;
3105 if (!typeok_scalar(op, mp, ltp, lt, rtp, rt))
3106 return false;
3107
3108 if (!typeok_op(op, arg, ln, ltp, lt, rn, rtp, rt))
3109 return false;
3110
3111 typeok_enum(op, mp, arg, ln, ltp, rn, rtp);
3112 return true;
3113 }
3114
3115 /* In traditional C, keep unsigned and promote FLOAT to DOUBLE. */
3116 static tspec_t
3117 promote_trad(tspec_t t)
3118 {
3119
3120 if (t == UCHAR || t == USHORT)
3121 return UINT;
3122 if (t == CHAR || t == SCHAR || t == SHORT)
3123 return INT;
3124 if (t == FLOAT)
3125 return DOUBLE;
3126 if (t == ENUM)
3127 return INT;
3128 return t;
3129 }
3130
3131 /*
3132 * C99 6.3.1.1p2 requires for types with lower rank than int that "If an int
3133 * can represent all the values of the original type, the value is converted
3134 * to an int; otherwise it is converted to an unsigned int", and that "All
3135 * other types are unchanged by the integer promotions".
3136 */
3137 static tspec_t
3138 promote_c90(const tnode_t *tn, tspec_t t, bool farg)
3139 {
3140 if (tn->tn_type->t_bitfield) {
3141 unsigned int width = tn->tn_type->t_bit_field_width;
3142 unsigned int int_width = size_in_bits(INT);
3143 // XXX: What about _Bool bit-fields, since C99?
3144 if (width < int_width)
3145 return INT;
3146 if (width == int_width)
3147 return is_uinteger(t) ? UINT : INT;
3148 return t;
3149 }
3150
3151 if (t == CHAR || t == SCHAR)
3152 return INT;
3153 if (t == UCHAR)
3154 return size_in_bits(CHAR) < size_in_bits(INT) ? INT : UINT;
3155 if (t == SHORT)
3156 return INT;
3157 if (t == USHORT)
3158 return size_in_bits(SHORT) < size_in_bits(INT) ? INT : UINT;
3159 if (t == ENUM)
3160 return INT;
3161 if (farg && t == FLOAT)
3162 return DOUBLE;
3163 return t;
3164 }
3165
3166 /*
3167 * Performs the "integer promotions" (C99 6.3.1.1p2), which convert small
3168 * integer types to either int or unsigned int.
3169 *
3170 * If allow_c90 is unset or the operand is a function argument with no type
3171 * information (no prototype or variable # of args), converts float to double.
3172 */
3173 tnode_t *
3174 promote(op_t op, bool farg, tnode_t *tn)
3175 {
3176
3177 tspec_t ot = tn->tn_type->t_tspec;
3178 if (!is_arithmetic(ot))
3179 return tn;
3180
3181 tspec_t nt = allow_c90 ? promote_c90(tn, ot, farg) : promote_trad(ot);
3182 if (nt == ot)
3183 return tn;
3184
3185 type_t *ntp = expr_dup_type(tn->tn_type);
3186 ntp->t_tspec = nt;
3187 /*
3188 * Keep t_is_enum even though t_tspec gets converted from ENUM to INT,
3189 * so we are later able to check compatibility of enum types.
3190 */
3191 return convert(op, 0, ntp, tn);
3192 }
3193
3194 static void
3195 convert_integer_from_floating(op_t op, const type_t *tp, const tnode_t *tn)
3196 {
3197
3198 if (op == CVT)
3199 /* cast from floating point '%s' to integer '%s' */
3200 query_message(2, type_name(tn->tn_type), type_name(tp));
3201 else
3202 /* implicit conversion from floating point '%s' to ... */
3203 query_message(1, type_name(tn->tn_type), type_name(tp));
3204 }
3205
3206 static bool
3207 should_warn_about_prototype_conversion(tspec_t nt,
3208 tspec_t ot, const tnode_t *ptn)
3209 {
3210
3211 if (nt == ot)
3212 return false;
3213
3214 if (nt == ENUM && ot == INT)
3215 return false;
3216
3217 if (is_floating(nt) != is_floating(ot) ||
3218 portable_rank_cmp(nt, ot) != 0) {
3219 /* representation and/or width change */
3220 if (!is_integer(ot))
3221 return true;
3222 /*
3223 * XXX: Investigate whether this rule makes sense; see
3224 * tests/usr.bin/xlint/lint1/platform_long.c.
3225 */
3226 return portable_rank_cmp(ot, INT) > 0;
3227 }
3228
3229 if (!hflag)
3230 return false;
3231
3232 /*
3233 * If the types differ only in sign and the argument has the same
3234 * representation in both types, print no warning.
3235 */
3236 if (ptn->tn_op == CON && is_integer(nt) &&
3237 signed_type(nt) == signed_type(ot) &&
3238 !msb(ptn->u.value.u.integer, ot))
3239 return false;
3240
3241 return true;
3242 }
3243
3244 /*
3245 * Warn if a prototype causes a type conversion that is different from what
3246 * would happen to the same argument in the absence of a prototype. This
3247 * check is intended for code that needs to stay compatible with pre-C90 C.
3248 *
3249 * Errors/warnings about illegal type combinations are already printed
3250 * in check_assign_types_compatible().
3251 */
3252 static void
3253 check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
3254 tnode_t *tn)
3255 {
3256
3257 if (!is_arithmetic(nt) || !is_arithmetic(ot))
3258 return;
3259
3260 /*
3261 * If the type of the formal parameter is char/short, a warning would
3262 * be useless, because functions declared the old style can't expect
3263 * char/short arguments.
3264 */
3265 if (nt == CHAR || nt == SCHAR || nt == UCHAR ||
3266 nt == SHORT || nt == USHORT)
3267 return;
3268
3269 tnode_t *ptn = promote(NOOP, true, tn);
3270 ot = ptn->tn_type->t_tspec;
3271
3272 if (should_warn_about_prototype_conversion(nt, ot, ptn)) {
3273 /* argument %d is converted from '%s' to '%s' ... */
3274 warning(259, arg, type_name(tn->tn_type), type_name(tp));
3275 }
3276 }
3277
3278 /*
3279 * When converting a large integer type to a small integer type, in some
3280 * cases the value of the actual expression is further restricted than the
3281 * type bounds, such as in (expr & 0xFF) or (expr % 100) or (expr >> 24).
3282 */
3283 static bool
3284 can_represent(const type_t *tp, const tnode_t *tn)
3285 {
3286
3287 debug_step("%s: type '%s'", __func__, type_name(tp));
3288 debug_node(tn);
3289
3290 uint64_t nmask = value_bits(width_in_bits(tp));
3291 if (!is_uinteger(tp->t_tspec))
3292 nmask >>= 1;
3293
3294 integer_constraints c = ic_expr(tn);
3295 if ((~c.bclr & ~nmask) == 0)
3296 return true;
3297
3298 integer_constraints tpc = ic_any(tp);
3299 if (is_uinteger(tp->t_tspec)
3300 ? tpc.umin <= c.umin && tpc.umax >= c.umax
3301 : tpc.smin <= c.smin && tpc.smax >= c.smax)
3302 return true;
3303
3304 return false;
3305 }
3306
3307 static bool
3308 should_warn_about_integer_conversion(const type_t *ntp, tspec_t nt,
3309 const tnode_t *otn, tspec_t ot)
3310 {
3311
3312 // XXX: The portable_rank_cmp aims at portable mode, independent of the
3313 // current platform, while can_represent acts on the actual type sizes
3314 // from the current platform. This mix is inconsistent, but anything
3315 // else would make the exact conditions too complicated to grasp.
3316 if (aflag > 0 && portable_rank_cmp(nt, ot) < 0) {
3317 if (ot == LONG || ot == ULONG
3318 || ot == LLONG || ot == ULLONG
3319 #ifdef INT128_SIZE
3320 || ot == INT128 || ot == UINT128
3321 #endif
3322 || aflag > 1)
3323 return !can_represent(ntp, otn);
3324 }
3325 return false;
3326 }
3327
3328 static void
3329 convert_integer_from_integer(op_t op, int arg, tspec_t nt, tspec_t ot,
3330 type_t *tp, tnode_t *tn)
3331 {
3332
3333 if (tn->tn_op == CON)
3334 return;
3335
3336 if (op == CVT)
3337 return;
3338
3339 if (Pflag && pflag && aflag > 0 &&
3340 portable_rank_cmp(nt, ot) > 0 &&
3341 is_uinteger(nt) != is_uinteger(ot)) {
3342 if (op == FARG)
3343 /* conversion to '%s' may sign-extend ... */
3344 warning(297, type_name(tp), arg);
3345 else
3346 /* conversion to '%s' may sign-extend ... */
3347 warning(131, type_name(tp));
3348 }
3349
3350 if (Pflag && portable_rank_cmp(nt, ot) > 0 &&
3351 (tn->tn_op == PLUS || tn->tn_op == MINUS || tn->tn_op == MULT ||
3352 tn->tn_op == SHL)) {
3353 /* suggest cast from '%s' to '%s' on op '%s' to ... */
3354 warning(324, type_name(gettyp(ot)), type_name(tp),
3355 op_name(tn->tn_op));
3356 }
3357
3358 if (should_warn_about_integer_conversion(tp, nt, tn, ot)) {
3359 if (op == FARG) {
3360 /* conversion from '%s' to '%s' may lose ... */
3361 warning(298,
3362 type_name(tn->tn_type), type_name(tp), arg);
3363 } else {
3364 /* conversion from '%s' to '%s' may lose accuracy */
3365 warning(132,
3366 type_name(tn->tn_type), type_name(tp));
3367 }
3368 }
3369
3370 if (any_query_enabled && is_uinteger(nt) != is_uinteger(ot))
3371 /* implicit conversion changes sign from '%s' to '%s' */
3372 query_message(3, type_name(tn->tn_type), type_name(tp));
3373 }
3374
3375 static void
3376 convert_integer_from_pointer(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
3377 {
3378
3379 if (tn->tn_op == CON)
3380 return;
3381 if (op != CVT)
3382 return; /* We already got an error. */
3383 if (portable_rank_cmp(nt, PTR) >= 0)
3384 return;
3385
3386 if (pflag && size_in_bits(nt) >= size_in_bits(PTR)) {
3387 /* conversion of pointer to '%s' may lose bits */
3388 warning(134, type_name(tp));
3389 } else {
3390 /* conversion of pointer to '%s' loses bits */
3391 warning(133, type_name(tp));
3392 }
3393 }
3394
3395 static bool
3396 struct_starts_with(const type_t *struct_tp, const type_t *member_tp)
3397 {
3398
3399 return struct_tp->u.sou->sou_first_member != NULL &&
3400 types_compatible(struct_tp->u.sou->sou_first_member->s_type,
3401 member_tp, true, false, NULL);
3402 }
3403
3404 static bool
3405 is_byte_array(const type_t *tp)
3406 {
3407
3408 return tp->t_tspec == ARRAY &&
3409 (tp->t_subt->t_tspec == CHAR || tp->t_subt->t_tspec == UCHAR);
3410 }
3411
3412 static bool
3413 union_contains(const type_t *utp, const type_t *mtp)
3414 {
3415 for (const sym_t *mem = utp->u.sou->sou_first_member;
3416 mem != NULL; mem = mem->s_next) {
3417 if (types_compatible(mem->s_type, mtp, true, false, NULL))
3418 return true;
3419 }
3420 return false;
3421 }
3422
3423 static bool
3424 should_warn_about_pointer_cast(const type_t *nstp, tspec_t nst,
3425 const type_t *ostp, tspec_t ost)
3426 {
3427
3428 while (nst == ARRAY)
3429 nstp = nstp->t_subt, nst = nstp->t_tspec;
3430 while (ost == ARRAY)
3431 ostp = ostp->t_subt, ost = ostp->t_tspec;
3432
3433 if (nst == STRUCT && ost == STRUCT &&
3434 (struct_starts_with(nstp, ostp) ||
3435 struct_starts_with(ostp, nstp)))
3436 return false;
3437
3438 if (is_incomplete(nstp) || is_incomplete(ostp))
3439 return false;
3440
3441 if (nst == CHAR || nst == UCHAR)
3442 return false; /* for the sake of traditional C code */
3443 if (ost == CHAR || ost == UCHAR)
3444 return false; /* for the sake of traditional C code */
3445
3446 /* Allow cast between pointers to sockaddr variants. */
3447 if (nst == STRUCT && ost == STRUCT) {
3448 const sym_t *nmem = nstp->u.sou->sou_first_member;
3449 const sym_t *omem = ostp->u.sou->sou_first_member;
3450 while (nmem != NULL && omem != NULL &&
3451 types_compatible(nmem->s_type, omem->s_type,
3452 true, false, NULL))
3453 nmem = nmem->s_next, omem = omem->s_next;
3454 if (nmem != NULL && is_byte_array(nmem->s_type))
3455 return false;
3456 if (omem != NULL && is_byte_array(omem->s_type))
3457 return false;
3458 if (nmem == NULL && omem == NULL)
3459 return false;
3460 }
3461
3462 if (nst == UNION || ost == UNION) {
3463 const type_t *union_tp = nst == UNION ? nstp : ostp;
3464 const type_t *other_tp = nst == UNION ? ostp : nstp;
3465 if (union_contains(union_tp, other_tp))
3466 return false;
3467 }
3468
3469 if (is_struct_or_union(nst) && is_struct_or_union(ost))
3470 return nstp->u.sou != ostp->u.sou;
3471
3472 enum rank_kind rk1 = type_properties(nst)->tt_rank_kind;
3473 enum rank_kind rk2 = type_properties(ost)->tt_rank_kind;
3474 if (rk1 != rk2 || rk1 == RK_NONE)
3475 return true;
3476
3477 return portable_rank_cmp(nst, ost) != 0;
3478 }
3479
3480 static void
3481 convert_pointer_from_pointer(type_t *ntp, tnode_t *tn)
3482 {
3483 const type_t *nstp = ntp->t_subt;
3484 const type_t *otp = tn->tn_type;
3485 const type_t *ostp = otp->t_subt;
3486 tspec_t nst = nstp->t_tspec;
3487 tspec_t ost = ostp->t_tspec;
3488
3489 if (nst == VOID || ost == VOID) {
3490 /* TODO: C99 behaves like C90 here. */
3491 if (!allow_trad && !allow_c99 && (nst == FUNC || ost == FUNC)) {
3492 const char *nts, *ots;
3493 /* null pointers are already handled in convert() */
3494 *(nst == FUNC ? &nts : &ots) = "function pointer";
3495 *(nst == VOID ? &nts : &ots) = "'void *'";
3496 /* conversion of %s to %s requires a cast */
3497 warning(303, ots, nts);
3498 }
3499 return;
3500 }
3501 if (nst == FUNC && ost == FUNC)
3502 return;
3503 if (nst == FUNC || ost == FUNC) {
3504 /* converting '%s' to '%s' is questionable */
3505 warning(229, type_name(otp), type_name(ntp));
3506 return;
3507 }
3508
3509 if (hflag && alignment_in_bits(nstp) > alignment_in_bits(ostp) &&
3510 ost != CHAR && ost != UCHAR &&
3511 !is_incomplete(ostp) &&
3512 !(nst == UNION && union_contains(nstp, ostp))) {
3513 /* converting '%s' to '%s' increases alignment ... */
3514 warning(135, type_name(otp), type_name(ntp),
3515 alignment_in_bits(ostp) / CHAR_SIZE,
3516 alignment_in_bits(nstp) / CHAR_SIZE);
3517 }
3518
3519 if (cflag && should_warn_about_pointer_cast(nstp, nst, ostp, ost)) {
3520 /* pointer cast from '%s' to '%s' may be troublesome */
3521 warning(247, type_name(otp), type_name(ntp));
3522 }
3523 }
3524
3525 /*
3526 * Insert a conversion operator, which converts the type of the node
3527 * to another given type.
3528 *
3529 * Possible values for 'op':
3530 * CVT a cast-expression
3531 * binary integer promotion for one of the operands, or a usual
3532 * arithmetic conversion
3533 * binary plain or compound assignments to bit-fields
3534 * FARG 'arg' is the number of the parameter (used for warnings)
3535 * NOOP several other implicit conversions
3536 * ...
3537 */
3538 tnode_t *
3539 convert(op_t op, int arg, type_t *tp, tnode_t *tn)
3540 {
3541 tspec_t nt = tp->t_tspec;
3542 tspec_t ot = tn->tn_type->t_tspec;
3543
3544 if (allow_trad && allow_c90 && op == FARG)
3545 check_prototype_conversion(arg, nt, ot, tp, tn);
3546
3547 if (nt == BOOL) {
3548 /* No further checks. */
3549
3550 } else if (is_integer(nt)) {
3551 if (ot == BOOL) {
3552 /* No further checks. */
3553 } else if (is_integer(ot))
3554 convert_integer_from_integer(op, arg, nt, ot, tp, tn);
3555 else if (is_floating(ot))
3556 convert_integer_from_floating(op, tp, tn);
3557 else if (ot == PTR)
3558 convert_integer_from_pointer(op, nt, tp, tn);
3559
3560 } else if (is_floating(nt)) {
3561 /* No further checks. */
3562
3563 } else if (nt == PTR) {
3564 if (is_null_pointer(tn)) {
3565 /* a null pointer may be assigned to any pointer. */
3566 } else if (ot == PTR && op == CVT)
3567 convert_pointer_from_pointer(tp, tn);
3568 }
3569
3570 tnode_t *ntn = expr_alloc_tnode();
3571 ntn->tn_op = CVT;
3572 ntn->tn_type = tp;
3573 ntn->tn_cast = op == CVT;
3574 ntn->tn_sys |= tn->tn_sys;
3575 ntn->u.ops.right = NULL;
3576 if (tn->tn_op != CON || nt == VOID) {
3577 ntn->u.ops.left = tn;
3578 } else {
3579 ntn->tn_op = CON;
3580 convert_constant(op, arg, ntn->tn_type, &ntn->u.value,
3581 &tn->u.value);
3582 }
3583
3584 return ntn;
3585 }
3586
3587 static void
3588 convert_constant_floating(op_t op, int arg, tspec_t ot, const type_t *tp,
3589 tspec_t nt, val_t *v, val_t *nv)
3590 {
3591 long double max = 0.0, min = 0.0;
3592
3593 switch (nt) {
3594 case CHAR:
3595 max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break;
3596 case UCHAR:
3597 max = TARG_UCHAR_MAX; min = 0; break;
3598 case SCHAR:
3599 max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break;
3600 case SHORT:
3601 max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break;
3602 case USHORT:
3603 max = TARG_USHRT_MAX; min = 0; break;
3604 case ENUM:
3605 case INT:
3606 max = TARG_INT_MAX; min = TARG_INT_MIN; break;
3607 case UINT:
3608 max = TARG_UINT_MAX; min = 0; break;
3609 case LONG:
3610 max = TARG_LONG_MAX; min = TARG_LONG_MIN; break;
3611 case ULONG:
3612 max = TARG_ULONG_MAX; min = 0; break;
3613 case LLONG:
3614 max = LLONG_MAX; min = LLONG_MIN; break;
3615 case ULLONG:
3616 max = ULLONG_MAX; min = 0; break;
3617 case FLOAT:
3618 case FCOMPLEX:
3619 max = FLT_MAX; min = -FLT_MAX; break;
3620 case DOUBLE:
3621 case DCOMPLEX:
3622 max = DBL_MAX; min = -DBL_MAX; break;
3623 case PTR:
3624 /* Already got an error because of float --> ptr */
3625 case LDOUBLE:
3626 case LCOMPLEX:
3627 /* LINTED 248 */
3628 max = LDBL_MAX; min = -max; break;
3629 default:
3630 lint_assert(/*CONSTCOND*/false);
3631 }
3632 if (v->u.floating > max || v->u.floating < min) {
3633 lint_assert(nt != LDOUBLE);
3634 if (op == FARG) {
3635 /* conversion of '%s' to '%s' is out of range, ... */
3636 warning(295,
3637 type_name(gettyp(ot)), type_name(tp), arg);
3638 } else {
3639 /* conversion of '%s' to '%s' is out of range */
3640 warning(119, type_name(gettyp(ot)), type_name(tp));
3641 }
3642 v->u.floating = v->u.floating > 0 ? max : min;
3643 }
3644
3645 if (nt == FLOAT || nt == FCOMPLEX)
3646 nv->u.floating = (float)v->u.floating;
3647 else if (nt == DOUBLE || nt == DCOMPLEX)
3648 nv->u.floating = (double)v->u.floating;
3649 else if (nt == LDOUBLE || nt == LCOMPLEX)
3650 nv->u.floating = v->u.floating;
3651 else
3652 nv->u.integer = (int64_t)v->u.floating;
3653 }
3654
3655 static bool
3656 convert_constant_to_floating(tspec_t nt, val_t *nv,
3657 tspec_t ot, const val_t *v)
3658 {
3659 if (nt == FLOAT) {
3660 nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
3661 (float)(uint64_t)v->u.integer : (float)v->u.integer;
3662 } else if (nt == DOUBLE) {
3663 nv->u.floating = (ot == PTR || is_uinteger(ot)) ?
3664 (double)(uint64_t)v->u.integer : (double)v->u.integer;
3665 } else if (nt == LDOUBLE) {
3666 nv->u.floating = (ot == PTR || is_uinteger(ot))
3667 ? (long double)(uint64_t)v->u.integer
3668 : (long double)v->u.integer;
3669 } else
3670 return false;
3671 return true;
3672 }
3673
3674 /*
3675 * Print a warning if bits which were set are lost due to the conversion.
3676 * This can happen with operator ORASS only.
3677 */
3678 static void
3679 convert_constant_check_range_bitor(size_t nsz, size_t osz, const val_t *v,
3680 uint64_t xmask, op_t op)
3681 {
3682 if (nsz < osz && (v->u.integer & xmask) != 0)
3683 /* constant truncated by conversion, op '%s' */
3684 warning(306, op_name(op));
3685 }
3686
3687 static void
3688 convert_constant_check_range_bitand(size_t nsz, size_t osz,
3689 uint64_t xmask, const val_t *nv,
3690 tspec_t ot, const val_t *v,
3691 const type_t *tp, op_t op)
3692 {
3693 if (nsz > osz &&
3694 (nv->u.integer & bit((unsigned int)(osz - 1))) != 0 &&
3695 (nv->u.integer & xmask) != xmask) {
3696 /* extra bits set to 0 in conversion of '%s' to '%s', ... */
3697 warning(309, type_name(gettyp(ot)),
3698 type_name(tp), op_name(op));
3699 } else if (nsz < osz &&
3700 (v->u.integer & xmask) != xmask &&
3701 (v->u.integer & xmask) != 0)
3702 /* constant truncated by conversion, op '%s' */
3703 warning(306, op_name(op));
3704 }
3705
3706 static void
3707 convert_constant_check_range_signed(op_t op, int arg)
3708 {
3709 if (op == ASSIGN)
3710 /* assignment of negative constant to unsigned type */
3711 warning(164);
3712 else if (op == INIT)
3713 /* initialization of unsigned with negative constant */
3714 warning(221);
3715 else if (op == FARG)
3716 /* conversion of negative constant to unsigned type, ... */
3717 warning(296, arg);
3718 else if (modtab[op].m_comparison) {
3719 /* handled by check_integer_comparison() */
3720 } else
3721 /* conversion of negative constant to unsigned type */
3722 warning(222);
3723 }
3724
3725 /*
3726 * Loss of significant bit(s). All truncated bits of unsigned types or all
3727 * truncated bits plus the msb of the target for signed types are considered
3728 * to be significant bits. Loss of significant bits means that at least one
3729 * of the bits was set in an unsigned type or that at least one but not all
3730 * of the bits was set in a signed type. Loss of significant bits means that
3731 * it is not possible, also not with necessary casts, to convert back to the
3732 * original type. An example for a necessary cast is:
3733 * char c; int i; c = 128;
3734 * i = c; ** yields -128 **
3735 * i = (unsigned char)c; ** yields 128 **
3736 */
3737 static void
3738 warn_constant_check_range_truncated(op_t op, int arg, const type_t *tp,
3739 tspec_t ot)
3740 {
3741 if (op == ASSIGN && tp->t_bitfield)
3742 /* precision lost in bit-field assignment */
3743 warning(166);
3744 else if (op == ASSIGN)
3745 /* constant truncated by assignment */
3746 warning(165);
3747 else if (op == INIT && tp->t_bitfield)
3748 /* bit-field initializer does not fit */
3749 warning(180);
3750 else if (op == INIT)
3751 /* initializer does not fit */
3752 warning(178);
3753 else if (op == CASE)
3754 /* case label affected by conversion */
3755 warning(196);
3756 else if (op == FARG)
3757 /* conversion of '%s' to '%s' is out of range, arg #%d */
3758 warning(295, type_name(gettyp(ot)), type_name(tp), arg);
3759 else
3760 /* conversion of '%s' to '%s' is out of range */
3761 warning(119, type_name(gettyp(ot)), type_name(tp));
3762 }
3763
3764 static void
3765 warn_constant_check_range_loss(op_t op, int arg, const type_t *tp,
3766 tspec_t ot)
3767 {
3768 if (op == ASSIGN && tp->t_bitfield)
3769 /* precision lost in bit-field assignment */
3770 warning(166);
3771 else if (op == INIT && tp->t_bitfield)
3772 /* bit-field initializer out of range */
3773 warning(11);
3774 else if (op == CASE)
3775 /* case label affected by conversion */
3776 warning(196);
3777 else if (op == FARG)
3778 /* conversion of '%s' to '%s' is out of range, arg #%d */
3779 warning(295, type_name(gettyp(ot)), type_name(tp), arg);
3780 else
3781 /* conversion of '%s' to '%s' is out of range */
3782 warning(119, type_name(gettyp(ot)), type_name(tp));
3783 }
3784
3785 static void
3786 convert_constant_check_range(tspec_t ot, const type_t *tp, tspec_t nt,
3787 op_t op, int arg, const val_t *v, val_t *nv)
3788 {
3789 unsigned int obitsz, nbitsz;
3790 uint64_t xmask, xmsk1;
3791
3792 obitsz = size_in_bits(ot);
3793 nbitsz = tp->t_bitfield ? tp->t_bit_field_width : size_in_bits(nt);
3794 xmask = value_bits(nbitsz) ^ value_bits(obitsz);
3795 xmsk1 = value_bits(nbitsz) ^ value_bits(obitsz - 1);
3796 if (op == ORASS || op == BITOR || op == BITXOR) {
3797 convert_constant_check_range_bitor(
3798 nbitsz, obitsz, v, xmask, op);
3799 } else if (op == ANDASS || op == BITAND) {
3800 convert_constant_check_range_bitand(
3801 nbitsz, obitsz, xmask, nv, ot, v, tp, op);
3802 } else if (nt != PTR && is_uinteger(nt) &&
3803 ot != PTR && !is_uinteger(ot) &&
3804 v->u.integer < 0)
3805 convert_constant_check_range_signed(op, arg);
3806 else if (nv->u.integer != v->u.integer && nbitsz <= obitsz &&
3807 (v->u.integer & xmask) != 0 &&
3808 (is_uinteger(ot) || (v->u.integer & xmsk1) != xmsk1))
3809 warn_constant_check_range_truncated(op, arg, tp, ot);
3810 else if (nv->u.integer != v->u.integer)
3811 warn_constant_check_range_loss(op, arg, tp, ot);
3812 }
3813
3814 /* Converts a typed constant to a constant of another type. */
3815 void
3816 convert_constant(op_t op, int arg, const type_t *ntp, val_t *nv, val_t *ov)
3817 {
3818 /*
3819 * TODO: make 'ov' const; the name of this function does not suggest
3820 * that it modifies 'ov'.
3821 */
3822 tspec_t ot = ov->v_tspec;
3823 tspec_t nt = nv->v_tspec = ntp->t_tspec;
3824 bool range_check = false;
3825
3826 if (nt == BOOL) { /* C99 6.3.1.2 */
3827 nv->v_unsigned_since_c90 = false;
3828 nv->u.integer = is_nonzero_val(ov) ? 1 : 0;
3829 return;
3830 }
3831
3832 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE)
3833 convert_constant_floating(op, arg, ot, ntp, nt, ov, nv);
3834 else if (!convert_constant_to_floating(nt, nv, ot, ov)) {
3835 range_check = true; /* Check for lost precision. */
3836 nv->u.integer = ov->u.integer;
3837 }
3838
3839 if (allow_trad && allow_c90 && ov->v_unsigned_since_c90 &&
3840 (is_floating(nt) || (
3841 (is_integer(nt) && !is_uinteger(nt) &&
3842 portable_rank_cmp(nt, ot) > 0)))) {
3843 /* C90 treats constant as unsigned */
3844 warning(157);
3845 ov->v_unsigned_since_c90 = false;
3846 }
3847
3848 if (is_integer(nt)) {
3849 unsigned int size = ntp->t_bitfield
3850 ? ntp->t_bit_field_width : size_in_bits(nt);
3851 nv->u.integer = convert_integer(nv->u.integer, nt, size);
3852 }
3853
3854 if (range_check && op != CVT)
3855 convert_constant_check_range(ot, ntp, nt, op, arg, ov, nv);
3856 }
3857
3858 tnode_t *
3859 build_sizeof(const type_t *tp)
3860 {
3861 unsigned int size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
3862 tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, size_in_bytes);
3863 tn->tn_system_dependent = true;
3864 debug_step("build_sizeof '%s' = %u", type_name(tp), size_in_bytes);
3865 return tn;
3866 }
3867
3868 tnode_t *
3869 build_offsetof(const type_t *tp, designation dn)
3870 {
3871 unsigned int offset_in_bits = 0;
3872
3873 if (!is_struct_or_union(tp->t_tspec)) {
3874 /* unacceptable operand of '%s' */
3875 error(111, "offsetof");
3876 goto proceed;
3877 }
3878 for (size_t i = 0; i < dn.dn_len; i++) {
3879 const designator *dr = dn.dn_items + i;
3880 if (dr->dr_kind == DK_SUBSCRIPT) {
3881 if (tp->t_tspec != ARRAY)
3882 goto proceed; /* silent error */
3883 tp = tp->t_subt;
3884 offset_in_bits += (unsigned) dr->dr_subscript
3885 * type_size_in_bits(tp);
3886 } else {
3887 if (!is_struct_or_union(tp->t_tspec))
3888 goto proceed; /* silent error */
3889 const char *name = dr->dr_member->s_name;
3890 sym_t *mem = find_member(tp->u.sou, name);
3891 if (mem == NULL) {
3892 /* type '%s' does not have member '%s' */
3893 error(101, name, type_name(tp));
3894 goto proceed;
3895 }
3896 tp = mem->s_type;
3897 offset_in_bits += mem->u.s_member.sm_offset_in_bits;
3898 }
3899 }
3900 free(dn.dn_items);
3901
3902 proceed:;
3903 unsigned int offset_in_bytes = offset_in_bits / CHAR_SIZE;
3904 tnode_t *tn = build_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
3905 tn->tn_system_dependent = true;
3906 return tn;
3907 }
3908
3909 unsigned int
3910 type_size_in_bits(const type_t *tp)
3911 {
3912
3913 unsigned int elem = 1;
3914 bool flex = false;
3915 lint_assert(tp != NULL);
3916 while (tp->t_tspec == ARRAY) {
3917 flex = true; /* allow c99 flex arrays [] [0] */
3918 elem *= tp->u.dimension;
3919 tp = tp->t_subt;
3920 }
3921 if (elem == 0 && !flex) {
3922 /* cannot take size/alignment of incomplete type */
3923 error(143);
3924 elem = 1;
3925 }
3926
3927 unsigned int elsz;
3928 switch (tp->t_tspec) {
3929 case VOID:
3930 /* cannot take size/alignment of void */
3931 error(146);
3932 elsz = 1;
3933 break;
3934 case FUNC:
3935 /* cannot take size/alignment of function type '%s' */
3936 error(144, type_name(tp));
3937 elsz = 1;
3938 break;
3939 case STRUCT:
3940 case UNION:
3941 if (is_incomplete(tp)) {
3942 /* cannot take size/alignment of incomplete type */
3943 error(143);
3944 elsz = 1;
3945 } else
3946 elsz = tp->u.sou->sou_size_in_bits;
3947 break;
3948 case ENUM:
3949 if (is_incomplete(tp)) {
3950 /* cannot take size/alignment of incomplete type */
3951 warning(143);
3952 }
3953 /* FALLTHROUGH */
3954 default:
3955 if (tp->t_bitfield)
3956 /* cannot take size/alignment of bit-field */
3957 error(145);
3958 elsz = size_in_bits(tp->t_tspec);
3959 lint_assert(elsz > 0);
3960 break;
3961 }
3962
3963 return elem * elsz;
3964 }
3965
3966 /* C11 6.5.3.4, GCC */
3967 tnode_t *
3968 build_alignof(const type_t *tp)
3969 {
3970 if (tp->t_tspec == FUNC) {
3971 /* cannot take size/alignment of function type '%s' */
3972 error(144, type_name(tp));
3973 return NULL;
3974 }
3975 if (tp->t_tspec == VOID) {
3976 /* cannot take size/alignment of void */
3977 error(146);
3978 return NULL;
3979 }
3980 if (is_incomplete(tp)) {
3981 /* cannot take size/alignment of incomplete type */
3982 error(143);
3983 return NULL;
3984 }
3985 if (tp->t_bitfield) {
3986 /* cannot take size/alignment of bit-field */
3987 error(145);
3988 return NULL;
3989 }
3990 return build_integer_constant(SIZEOF_TSPEC,
3991 (int64_t)alignment_in_bits(tp) / CHAR_SIZE);
3992 }
3993
3994 static tnode_t *
3995 cast_to_union(tnode_t *otn, bool sys, type_t *ntp)
3996 {
3997
3998 if (!allow_gcc) {
3999 /* union cast is a GCC extension */
4000 error(328);
4001 return NULL;
4002 }
4003
4004 for (const sym_t *m = ntp->u.sou->sou_first_member;
4005 m != NULL; m = m->s_next) {
4006 if (types_compatible(m->s_type, otn->tn_type,
4007 false, false, NULL)) {
4008 tnode_t *ntn = build_op(CVT, sys, ntp, otn, NULL);
4009 ntn->tn_cast = true;
4010 return ntn;
4011 }
4012 }
4013
4014 /* type '%s' is not a member of '%s' */
4015 error(329, type_name(otn->tn_type), type_name(ntp));
4016 return NULL;
4017 }
4018
4019 tnode_t *
4020 cast(tnode_t *tn, bool sys, type_t *tp)
4021 {
4022
4023 if (tn == NULL)
4024 return NULL;
4025
4026 tn = cconv(tn);
4027
4028 lint_assert(tp != NULL);
4029 tspec_t nt = tp->t_tspec;
4030 tspec_t ot = tn->tn_type->t_tspec;
4031
4032 if (nt == VOID) {
4033 /*
4034 * C90 6.3.4, C99 6.5.4p2 and C11 6.5.4p2 allow any type to be
4035 * cast to void. The only other allowed casts are from a
4036 * scalar type to a scalar type.
4037 */
4038 } else if (nt == UNION)
4039 return cast_to_union(tn, sys, tp);
4040 else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
4041 /* Casting to a struct is an undocumented GCC extension. */
4042 if (!(allow_gcc && nt == STRUCT))
4043 goto invalid_cast;
4044 } else if (is_struct_or_union(ot))
4045 goto invalid_cast;
4046 else if (ot == VOID) {
4047 /* improper cast of void expression */
4048 error(148);
4049 return NULL;
4050 } else if (is_integer(nt) && is_scalar(ot)) {
4051 /* ok */
4052 } else if (is_floating(nt) && is_arithmetic(ot)) {
4053 /* ok */
4054 } else if (nt == PTR && is_integer(ot)) {
4055 /* ok */
4056 } else if (nt == PTR && ot == PTR) {
4057 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
4058 if (hflag)
4059 /* cast discards 'const' from type '%s' */
4060 warning(275, type_name(tn->tn_type));
4061 }
4062 } else
4063 goto invalid_cast;
4064
4065 if (any_query_enabled
4066 && types_compatible(tp, tn->tn_type, false, false, NULL))
4067 /* no-op cast from '%s' to '%s' */
4068 query_message(6, type_name(tn->tn_type), type_name(tp));
4069
4070 tn = convert(CVT, 0, tp, tn);
4071 tn->tn_cast = true;
4072 tn->tn_sys = sys;
4073
4074 return tn;
4075
4076 invalid_cast:
4077 /* invalid cast from '%s' to '%s' */
4078 error(147, type_name(tn->tn_type), type_name(tp));
4079 return NULL;
4080 }
4081
4082 void
4083 add_function_argument(function_call *call, tnode_t *arg)
4084 {
4085 /*
4086 * If there was a serious error in the expression for the argument,
4087 * create a dummy argument so the positions of the remaining arguments
4088 * will not change.
4089 */
4090 if (arg == NULL)
4091 arg = build_integer_constant(INT, 0);
4092
4093 if (call->args_len >= call->args_cap) {
4094 call->args_cap += 8;
4095 tnode_t **new_args = expr_zero_alloc(
4096 call->args_cap * sizeof(*call->args),
4097 "function_call.args");
4098 memcpy(new_args, call->args,
4099 call->args_len * sizeof(*call->args));
4100 call->args = new_args;
4101 }
4102 call->args[call->args_len++] = arg;
4103 }
4104
4105 /*
4106 * Compare the type of an argument with the corresponding type of a
4107 * prototype parameter. If it is a valid combination, but both types
4108 * are not the same, insert a conversion to convert the argument into
4109 * the type of the parameter.
4110 */
4111 static tnode_t *
4112 check_prototype_argument(
4113 int n, /* pos of arg */
4114 type_t *tp, /* expected type (from prototype) */
4115 tnode_t *tn) /* argument */
4116 {
4117 tnode_t *ln = xcalloc(1, sizeof(*ln));
4118 ln->tn_type = expr_unqualified_type(tp);
4119 ln->tn_lvalue = true;
4120 if (typeok(FARG, n, ln, tn)) {
4121 bool dowarn;
4122 if (!types_compatible(tp, tn->tn_type,
4123 true, false, (dowarn = false, &dowarn)) || dowarn)
4124 tn = convert(FARG, n, tp, tn);
4125 }
4126 free(ln);
4127 return tn;
4128 }
4129
4130 /*
4131 * Check types of all function arguments and insert conversions,
4132 * if necessary.
4133 */
4134 static bool
4135 check_function_arguments(const function_call *call)
4136 {
4137 type_t *ftp = call->func->tn_type->t_subt;
4138
4139 /* get # of parameters in the prototype */
4140 int npar = 0;
4141 for (const sym_t *p = ftp->u.params; p != NULL; p = p->s_next)
4142 npar++;
4143
4144 int narg = (int)call->args_len;
4145
4146 const sym_t *param = ftp->u.params;
4147 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
4148 /* argument mismatch: %d %s passed, %d expected */
4149 error(150, narg, narg != 1 ? "arguments" : "argument", npar);
4150 param = NULL;
4151 }
4152
4153 for (int n = 1; n <= narg; n++) {
4154 tnode_t *arg = call->args[n - 1];
4155
4156 /* some things which are always not allowed */
4157 tspec_t at = arg->tn_type->t_tspec;
4158 if (at == VOID) {
4159 /* void expressions may not be arguments, arg #%d */
4160 error(151, n);
4161 return false;
4162 }
4163 if (is_struct_or_union(at) &&
4164 is_incomplete(arg->tn_type)) {
4165 /* argument cannot have unknown size, arg #%d */
4166 error(152, n);
4167 return false;
4168 }
4169 if (is_integer(at) &&
4170 arg->tn_type->t_is_enum &&
4171 is_incomplete(arg->tn_type)) {
4172 /* argument cannot have unknown size, arg #%d */
4173 warning(152, n);
4174 }
4175
4176 arg = cconv(arg);
4177 call->args[n - 1] = arg;
4178
4179 arg = param != NULL
4180 ? check_prototype_argument(n, param->s_type, arg)
4181 : promote(NOOP, true, arg);
4182 call->args[n - 1] = arg;
4183
4184 if (param != NULL)
4185 param = param->s_next;
4186 }
4187
4188 return true;
4189 }
4190
4191 tnode_t *
4192 build_function_call(tnode_t *func, bool sys, function_call *call)
4193 {
4194
4195 if (func == NULL)
4196 return NULL;
4197
4198 op_t fcop = func->tn_op == NAME && func->tn_type->t_tspec == FUNC
4199 ? CALL : ICALL;
4200
4201 call->func = func;
4202 check_ctype_function_call(call);
4203
4204 func = cconv(func);
4205 call->func = func;
4206
4207 if (func->tn_type->t_tspec != PTR ||
4208 func->tn_type->t_subt->t_tspec != FUNC) {
4209 /* cannot call '%s', must be a function */
4210 error(149, type_name(func->tn_type));
4211 return NULL;
4212 }
4213
4214 if (!check_function_arguments(call))
4215 call->args = NULL;
4216
4217 tnode_t *ntn = expr_alloc_tnode();
4218 ntn->tn_op = fcop;
4219 ntn->tn_type = func->tn_type->t_subt->t_subt;
4220 ntn->tn_sys = sys;
4221 ntn->u.call = call;
4222 return ntn;
4223 }
4224
4225 /*
4226 * Return the value of an integral constant expression.
4227 * If the expression is not constant or its type is not an integer
4228 * type, an error message is printed.
4229 */
4230 val_t *
4231 integer_constant(tnode_t *tn, bool required)
4232 {
4233
4234 if (tn != NULL)
4235 tn = cconv(tn);
4236 if (tn != NULL)
4237 tn = promote(NOOP, false, tn);
4238
4239 val_t *v = xcalloc(1, sizeof(*v));
4240
4241 if (tn == NULL) {
4242 lint_assert(seen_error);
4243 debug_step("constant node is null; returning 1 instead");
4244 v->v_tspec = INT;
4245 v->u.integer = 1;
4246 return v;
4247 }
4248
4249 v->v_tspec = tn->tn_type->t_tspec;
4250
4251 if (tn->tn_op == CON) {
4252 lint_assert(tn->tn_type->t_tspec == tn->u.value.v_tspec);
4253 if (is_integer(tn->u.value.v_tspec)) {
4254 v->v_unsigned_since_c90 =
4255 tn->u.value.v_unsigned_since_c90;
4256 v->u.integer = tn->u.value.u.integer;
4257 return v;
4258 }
4259 v->u.integer = (int64_t)tn->u.value.u.floating;
4260 } else
4261 v->u.integer = 1;
4262
4263 if (required)
4264 /* integral constant expression expected */
4265 error(55);
4266 else
4267 /* variable array dimension is a C99/GCC extension */
4268 c99ism(318);
4269
4270 if (!is_integer(v->v_tspec))
4271 v->v_tspec = INT;
4272
4273 return v;
4274 }
4275
4276 static bool
4277 is_constcond_false(const tnode_t *tn, tspec_t t)
4278 {
4279 return (t == BOOL || t == INT) &&
4280 tn->tn_op == CON && tn->u.value.u.integer == 0;
4281 }
4282
4283 /*
4284 * Perform some tests on expressions which can't be done in build_binary()
4285 * and functions called by build_binary(). These tests must be done here
4286 * because we need some information about the context in which the operations
4287 * are performed.
4288 * After all tests are performed and dofreeblk is true, expr() frees the
4289 * memory which is used for the expression.
4290 */
4291 void
4292 expr(tnode_t *tn, bool vctx, bool cond, bool dofreeblk, bool is_do_while)
4293 {
4294
4295 if (tn == NULL) { /* in case of errors */
4296 expr_free_all();
4297 return;
4298 }
4299
4300 /* expr() is also called in global initializations */
4301 if (dcs->d_kind != DLK_EXTERN && !is_do_while)
4302 check_statement_reachable();
4303
4304 check_expr_misc(tn, vctx, cond, !cond, false, false, false);
4305 if (tn->tn_op == ASSIGN && !tn->tn_parenthesized) {
4306 if (hflag && cond)
4307 /* assignment in conditional context */
4308 warning(159);
4309 } else if (tn->tn_op == CON) {
4310 if (hflag && cond && !suppress_constcond &&
4311 !tn->tn_system_dependent &&
4312 !(is_do_while &&
4313 is_constcond_false(tn, tn->tn_type->t_tspec)))
4314 /* constant in conditional context */
4315 warning(161);
4316 }
4317 if (!modtab[tn->tn_op].m_has_side_effect) {
4318 /*
4319 * for left operands of COMMA this warning is already printed
4320 */
4321 if (tn->tn_op != COMMA && !vctx && !cond)
4322 check_null_effect(tn);
4323 }
4324 debug_node(tn);
4325
4326 if (dofreeblk)
4327 expr_free_all();
4328 }
4329
4330 /* If the expression has the form '*(arr + idx)', check the array index. */
4331 static void
4332 check_array_index(const tnode_t *indir, bool taking_address)
4333 {
4334 const tnode_t *plus, *arr, *idx;
4335
4336 if (indir->tn_op == INDIR
4337 && (plus = indir->u.ops.left, plus->tn_op == PLUS)
4338 && plus->u.ops.left->tn_op == ADDR
4339 && (arr = plus->u.ops.left->u.ops.left, true)
4340 && (arr->tn_op == STRING || arr->tn_op == NAME)
4341 && arr->tn_type->t_tspec == ARRAY
4342 && (idx = plus->u.ops.right, idx->tn_op == CON)
4343 && (!is_incomplete(arr->tn_type) || idx->u.value.u.integer < 0))
4344 goto proceed;
4345 return;
4346
4347 proceed:;
4348 int elsz = length_in_bits(arr->tn_type->t_subt, NULL);
4349 if (elsz == 0)
4350 return;
4351 elsz /= CHAR_SIZE;
4352
4353 /* Change the unit of the index from bytes to element size. */
4354 int64_t con = is_uinteger(idx->tn_type->t_tspec)
4355 ? (int64_t)((uint64_t)idx->u.value.u.integer / elsz)
4356 : idx->u.value.u.integer / elsz;
4357
4358 int dim = arr->tn_type->u.dimension + (taking_address ? 1 : 0);
4359
4360 if (!is_uinteger(idx->tn_type->t_tspec) && con < 0)
4361 /* array subscript cannot be negative: %ld */
4362 warning(167, (long)con);
4363 else if (dim > 0 && (uint64_t)con >= (uint64_t)dim)
4364 /* array subscript cannot be > %d: %ld */
4365 warning(168, dim - 1, (long)con);
4366 }
4367
4368 static void
4369 check_expr_addr(const tnode_t *ln, bool szof, bool fcall)
4370 {
4371 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4372 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4373 if (!szof)
4374 mark_as_set(ln->u.sym);
4375 mark_as_used(ln->u.sym, fcall, szof);
4376 }
4377 check_array_index(ln, true);
4378 }
4379
4380 /*
4381 * If there is an asm statement in one of the compound statements around,
4382 * there may be other side effects, so don't warn.
4383 */
4384 static bool
4385 is_asm_around(void)
4386 {
4387 for (decl_level *dl = dcs; dl != NULL; dl = dl->d_enclosing)
4388 if (dl->d_asm)
4389 return true;
4390 return false;
4391 }
4392
4393 static void
4394 check_expr_side_effect(const tnode_t *ln, bool szof)
4395 {
4396
4397 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4398 if (ln->tn_op == NAME && (reached || !warn_about_unreachable)) {
4399 scl_t sc = ln->u.sym->s_scl;
4400 if (sc != EXTERN && sc != STATIC &&
4401 !ln->u.sym->s_set && !szof && !is_asm_around()) {
4402 /* '%s' may be used before set */
4403 warning(158, ln->u.sym->s_name);
4404 mark_as_set(ln->u.sym);
4405 }
4406 mark_as_used(ln->u.sym, false, false);
4407 }
4408 }
4409
4410 static void
4411 check_expr_assign(const tnode_t *ln, bool szof)
4412 {
4413 /* XXX: Taking warn_about_unreachable into account here feels wrong. */
4414 if (ln->tn_op == NAME && !szof && (reached || !warn_about_unreachable)) {
4415 mark_as_set(ln->u.sym);
4416 if (ln->u.sym->s_scl == EXTERN)
4417 outusg(ln->u.sym);
4418 }
4419 check_array_index(ln, false);
4420 }
4421
4422 static void
4423 check_expr_call(const tnode_t *tn, const tnode_t *ln,
4424 bool szof, bool vctx, bool cond, bool retval_discarded)
4425 {
4426 lint_assert(ln->tn_op == ADDR);
4427 lint_assert(ln->u.ops.left->tn_op == NAME);
4428 if (!szof && !is_compiler_builtin(ln->u.ops.left->u.sym->s_name))
4429 outcall(tn, vctx || cond, retval_discarded);
4430 check_snprintb(tn);
4431 }
4432
4433 static void
4434 check_expr_op(op_t op, const tnode_t *ln, bool szof, bool fcall, bool eqwarn)
4435 {
4436 switch (op) {
4437 case ADDR:
4438 check_expr_addr(ln, szof, fcall);
4439 break;
4440 case LOAD:
4441 check_array_index(ln, false);
4442 /* FALLTHROUGH */
4443 case INCBEF:
4444 case DECBEF:
4445 case INCAFT:
4446 case DECAFT:
4447 case ADDASS:
4448 case SUBASS:
4449 case MULASS:
4450 case DIVASS:
4451 case MODASS:
4452 case ANDASS:
4453 case ORASS:
4454 case XORASS:
4455 case SHLASS:
4456 case SHRASS:
4457 case REAL:
4458 case IMAG:
4459 check_expr_side_effect(ln, szof);
4460 break;
4461 case ASSIGN:
4462 check_expr_assign(ln, szof);
4463 break;
4464 case EQ:
4465 if (hflag && eqwarn)
4466 /* operator '==' found where '=' was expected */
4467 warning(160);
4468 break;
4469 default:
4470 break;
4471 }
4472 }
4473
4474 /*
4475 * vctx ???
4476 * cond whether the expression is a condition that
4477 * will be compared with 0
4478 * eqwarn whether the operator '==' might be a
4479 * misspelled '='
4480 * fcall whether the expression is a function call
4481 * retval_discarded whether the return value of a function call
4482 * is discarded; such calls will be analyzed by
4483 * lint2 in messages 4, 8 and 9
4484 * szof whether the expression is part of a sizeof
4485 * expression, which means that its value is
4486 * discarded since only the type is relevant
4487 */
4488 void
4489 check_expr_misc(const tnode_t *tn, bool vctx, bool cond,
4490 bool eqwarn, bool fcall, bool retval_discarded, bool szof)
4491 {
4492
4493 if (tn == NULL)
4494 return;
4495 op_t op = tn->tn_op;
4496 if (op == NAME || op == CON || op == STRING)
4497 return;
4498 if (op == CALL || op == ICALL) {
4499 const function_call *call = tn->u.call;
4500 if (op == CALL)
4501 check_expr_call(tn, call->func,
4502 szof, vctx, cond, retval_discarded);
4503 bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
4504 check_expr_misc(call->func, false, false, false, op == CALL,
4505 discard, szof);
4506 if (call->args != NULL) {
4507 for (size_t i = 0, n = call->args_len; i < n; i++)
4508 check_expr_misc(call->args[i],
4509 false, false, false, false, false, szof);
4510 }
4511 return;
4512 }
4513
4514 lint_assert(has_operands(tn));
4515 tnode_t *ln = tn->u.ops.left;
4516 tnode_t *rn = tn->u.ops.right;
4517 check_expr_op(op, ln, szof, fcall, eqwarn);
4518
4519 const mod_t *mp = &modtab[op];
4520 bool cvctx = mp->m_value_context;
4521 bool ccond = mp->m_compares_with_zero;
4522 bool eq = mp->m_warn_if_operand_eq &&
4523 !ln->tn_parenthesized &&
4524 rn != NULL && !rn->tn_parenthesized;
4525
4526 /*
4527 * Values of operands of ':' are not used if the type of at least
4528 * one of the operands (for GCC compatibility) is 'void'.
4529 *
4530 * XXX test/value context of QUEST should probably be used as
4531 * context for both operands of COLON.
4532 */
4533 if (op == COLON && tn->tn_type->t_tspec == VOID)
4534 cvctx = ccond = false;
4535 bool discard = op == CVT && tn->tn_type->t_tspec == VOID;
4536 check_expr_misc(ln, cvctx, ccond, eq, op == CALL, discard, szof);
4537
4538 switch (op) {
4539 case LOGAND:
4540 case LOGOR:
4541 check_expr_misc(rn, false, true, eq, false, false, szof);
4542 break;
4543 case COLON:
4544 check_expr_misc(rn, cvctx, ccond, eq, false, false, szof);
4545 break;
4546 case COMMA:
4547 check_expr_misc(rn, vctx, cond, false, false, false, szof);
4548 break;
4549 default:
4550 if (mp->m_binary)
4551 check_expr_misc(rn, true, false, eq, false, false,
4552 szof);
4553 break;
4554 }
4555 }
4556
4557 /*
4558 * Return whether the expression can be used for static initialization.
4559 *
4560 * Constant initialization expressions must be constant or an address
4561 * of a static object with an optional offset. In the first case,
4562 * the result is returned in *offsp. In the second case, the static
4563 * object is returned in *symp and the offset in *offsp.
4564 *
4565 * The expression can consist of PLUS, MINUS, ADDR, NAME, STRING and
4566 * CON. Type conversions are allowed if they do not change binary
4567 * representation (including width).
4568 *
4569 * C99 6.6 "Constant expressions"
4570 * C99 6.7.8p4 restricts initializers for static storage duration
4571 */
4572 bool
4573 constant_addr(const tnode_t *tn, const sym_t **symp, ptrdiff_t *offsp)
4574 {
4575 const sym_t *sym;
4576 ptrdiff_t offs1, offs2;
4577 tspec_t t, ot;
4578
4579 switch (tn->tn_op) {
4580 case MINUS:
4581 if (tn->u.ops.right->tn_op == CVT)
4582 return constant_addr(tn->u.ops.right, symp, offsp);
4583 else if (tn->u.ops.right->tn_op != CON)
4584 return false;
4585 /* FALLTHROUGH */
4586 case PLUS:
4587 offs1 = offs2 = 0;
4588 if (tn->u.ops.left->tn_op == CON) {
4589 offs1 = (ptrdiff_t)tn->u.ops.left->u.value.u.integer;
4590 if (!constant_addr(tn->u.ops.right, &sym, &offs2))
4591 return false;
4592 } else if (tn->u.ops.right->tn_op == CON) {
4593 offs2 = (ptrdiff_t)tn->u.ops.right->u.value.u.integer;
4594 if (tn->tn_op == MINUS)
4595 offs2 = -offs2;
4596 if (!constant_addr(tn->u.ops.left, &sym, &offs1))
4597 return false;
4598 } else {
4599 return false;
4600 }
4601 *symp = sym;
4602 *offsp = offs1 + offs2;
4603 return true;
4604 case ADDR:
4605 if (tn->u.ops.left->tn_op == NAME) {
4606 *symp = tn->u.ops.left->u.sym;
4607 *offsp = 0;
4608 return true;
4609 } else {
4610 /*
4611 * If this were the front end of a compiler, we would
4612 * return a label instead of 0, at least if
4613 * 'tn->u.ops.left->tn_op == STRING'.
4614 */
4615 *symp = NULL;
4616 *offsp = 0;
4617 return true;
4618 }
4619 case CVT:
4620 t = tn->tn_type->t_tspec;
4621 ot = tn->u.ops.left->tn_type->t_tspec;
4622 if ((!is_integer(t) && t != PTR) ||
4623 (!is_integer(ot) && ot != PTR)) {
4624 return false;
4625 }
4626 #if 0
4627 /*-
4628 * consider:
4629 * struct foo {
4630 * unsigned char a;
4631 * } f = {
4632 * (unsigned char)(unsigned long)
4633 * (&(((struct foo *)0)->a))
4634 * };
4635 * since psize(unsigned long) != psize(unsigned char),
4636 * this fails.
4637 */
4638 else if (psize(t) != psize(ot))
4639 return -1;
4640 #endif
4641 return constant_addr(tn->u.ops.left, symp, offsp);
4642 default:
4643 return false;
4644 }
4645 }
4646
4647 /* Append s2 to s1, then free s2. */
4648 buffer *
4649 cat_strings(buffer *s1, buffer *s2)
4650 {
4651
4652 if ((s1->data != NULL) != (s2->data != NULL)) {
4653 /* cannot concatenate wide and regular string literals */
4654 error(292);
4655 return s1;
4656 }
4657
4658 if (s1->data != NULL) {
4659 while (s1->len + s2->len + 1 > s1->cap)
4660 s1->cap *= 2;
4661 s1->data = xrealloc(s1->data, s1->cap);
4662 memcpy(s1->data + s1->len, s2->data, s2->len + 1);
4663 free(s2->data);
4664 }
4665 s1->len += s2->len;
4666 free(s2);
4667
4668 return s1;
4669 }
4670
4671
4672 typedef struct stmt_expr {
4673 memory_pool se_mem;
4674 sym_t *se_sym;
4675 struct stmt_expr *se_enclosing;
4676 } stmt_expr;
4677
4678 static stmt_expr *stmt_exprs;
4679
4680 void
4681 begin_statement_expr(void)
4682 {
4683 debug_enter();
4684
4685 stmt_expr *se = xmalloc(sizeof(*se));
4686 se->se_mem = expr_save_memory();
4687 se->se_sym = NULL;
4688 se->se_enclosing = stmt_exprs;
4689 stmt_exprs = se;
4690 }
4691
4692 void
4693 do_statement_expr(tnode_t *tn)
4694 {
4695 block_level--;
4696 mem_block_level--;
4697 stmt_exprs->se_sym = tn != NULL
4698 ? mktempsym(block_dup_type(tn->tn_type))
4699 : NULL; /* after a syntax error */
4700 mem_block_level++;
4701 block_level++;
4702 /* '({ ... })' is a GCC extension */
4703 gnuism(320);
4704 }
4705
4706 tnode_t *
4707 end_statement_expr(void)
4708 {
4709 tnode_t *tn;
4710
4711 stmt_expr *se = stmt_exprs;
4712 if (se->se_sym == NULL) {
4713 tn = NULL; /* after a syntax error */
4714 goto end;
4715 }
4716
4717 tn = build_name(se->se_sym, false);
4718 (void)expr_save_memory(); /* leak */
4719 expr_restore_memory(se->se_mem);
4720 stmt_exprs = se->se_enclosing;
4721 free(se);
4722
4723 end:
4724 debug_leave();
4725 return tn;
4726 }
4727
4728 bool
4729 in_statement_expr(void)
4730 {
4731 return stmt_exprs != NULL;
4732 }
4733