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