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