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