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