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