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