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