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