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