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